一个接一个地移动矩形

时间:2014-06-25 18:10:48

标签: c# xna

我在XNA程序中制作了几个矩形。下一步是选择它们并逐个移动它们。

要选择它们,我使用代码

MouseState mouse = Mouse.GetState();

bool IsSelect1=false;
bool IsSelect2 = false;
bool IsSelect3 = false;
int IsSelectNum=0;

//To set the value of IsSelectNum
if ((mouse.X > drawRectangle.X && mouse.X <= mouse.X + currentCharacter.Width
   && mouse.Y >= drawRectangle.Y && mouse.Y <= drawRectangle.Y + currentCharacter.Height) 
   && (mouse.LeftButton == ButtonState.Pressed))
            { IsSelectNum = 1; }

else if ((mouse.X > drawRectangle2.X && mouse.X <= mouse.X + currentCharacter2.Width
   && mouse.Y >= drawRectangle2.Y && mouse.Y <= drawRectangle2.Y + currentCharacter2.Height)
   && (mouse.LeftButton == ButtonState.Pressed))
            { IsSelectNum = 2; }

else if ((mouse.X > drawRectangle3.X && mouse.X <= mouse.X + currentCharacter3.Width
   && mouse.Y >= drawRectangle3.Y && mouse.Y <= drawRectangle3.Y + currentCharacter3.Height)
   && (mouse.LeftButton == ButtonState.Pressed))
            { IsSelectNum = 3; }

//To choose figure using switch function
switch (IsSelectNum)
{
    case 1:
        IsSelect1 = true;
        break;
    case 2:
        IsSelect2 = true;
        break;
    case 3:
        IsSelect3 = true;
        break;
    default:
        break;
}

为了移动它们,我使用

    //If selective, then it can be moved
    if (IsSelect1 == true)
    {
        drawRectangle.X = mouse.X - currentCharacter.Width / 2;
        drawRectangle.Y = mouse.Y - currentCharacter.Width / 2;

        //To finally locate figure in the top
        if (drawRectangle.X <= 70 && drawRectangle.Y <= 15)
        {
            drawRectangle.X=50;
            drawRectangle.Y=10;
            IsSelect1 = false;
            IsSelectNum = 0;
        }
    }

    else if(IsSelect2==true)
   {
    drawRectangle2.X = mouse.X - currentCharacter2.Width / 2;
    drawRectangle2.Y = mouse.Y - currentCharacter2.Width / 2;
    //To finally locate figure in the top
    if (drawRectangle2.X  >= 250 && drawRectangle2.X <= 320 && drawRectangle2.Y <= 15)
    {
        drawRectangle2.X = 280;
        drawRectangle2.Y = 10;
        IsSelect2 = false;
        IsSelectNum = 0;
    }
   }

 else if (IsSelect3 == true)
   {
    drawRectangle3.X = mouse.X - currentCharacter3.Width / 2;
    drawRectangle3.Y = mouse.Y - currentCharacter3.Width / 2;
    //To finally locate figure in the top
    if (drawRectangle3.X >= 400 && drawRectangle3.X <= 520 && drawRectangle3.Y <= 15)
    {
        drawRectangle3.X = 510;
        drawRectangle3.Y = 10;
        IsSelect3 = false;
        IsSelectNum = 0;
    }

   }

然而,有些事情在这里并不完全正确。移动一个矩形后,想要移动另一个矩形。但是我无法取消选择之前的矩形。我该怎么改呢?

更多更新。谢谢dbc,我修复了我的代码

我修复了这个代码  `bool Select1 = true;             bool Select2 = true;             bool Select3 = true;

        int IsSelectNum=0;

        //To set the value of IsSelectNum
        if ((mouse.X > drawRectangle.X && mouse.X <= drawRectangle.X + currentCharacter.Width
           && mouse.Y >= drawRectangle.Y && mouse.Y <= drawRectangle.Y + currentCharacter.Height) 
           && (mouse.LeftButton == ButtonState.Pressed))
        { IsSelectNum = 1; }

        else if ((mouse.X > drawRectangle2.X && mouse.X <= drawRectangle2.X + currentCharacter2.Width
           && mouse.Y >= drawRectangle2.Y && mouse.Y <= drawRectangle2.Y + currentCharacter2.Height)
           && (mouse.LeftButton == ButtonState.Pressed))
                    { IsSelectNum = 2; }

        else if ((mouse.X > drawRectangle3.X && mouse.X <= drawRectangle3.X + currentCharacter3.Width
           && mouse.Y >= drawRectangle3.Y && mouse.Y <= drawRectangle3.Y + currentCharacter3.Height)
           && (mouse.LeftButton == ButtonState.Pressed))
                    { IsSelectNum = 3; }

        //To choose figure using switch function
        switch (IsSelectNum)
        {
            case 1:
                if(Select1){
             drawRectangle.X = mouse.X - currentCharacter.Width / 2;
             drawRectangle.Y = mouse.Y - currentCharacter.Width / 2;

                    //To finally locate figure in the top
                    if (drawRectangle.X <= 70 && drawRectangle.Y <= 15)
                    {
                        drawRectangle.X = 50;
                        drawRectangle.Y = 10;
                        Select1 = false;

                    }
                }
                break;

            case 2:
                if (Select2)
                {
                    drawRectangle2.X = mouse.X - currentCharacter2.Width / 2;
                    drawRectangle2.Y = mouse.Y - currentCharacter2.Width / 2;
                    //To finally locate figure in the top
                    if (drawRectangle2.X >= 250 && drawRectangle2.X <= 320 && drawRectangle2.Y <= 15)
                    {
                        drawRectangle2.X = 280;
                        drawRectangle2.Y = 10;
                        Select2 = false;
                    }
                }
                break;

            case 3:
                if (Select3)
                {
                    drawRectangle3.X = mouse.X - currentCharacter3.Width / 2;
                    drawRectangle3.Y = mouse.Y - currentCharacter3.Width / 2;
                    //To finally locate figure in the top
                    if (drawRectangle3.X >= 400 && drawRectangle3.X <= 520 && drawRectangle3.Y <= 15)
                    {
                        drawRectangle3.X = 510;
                        drawRectangle3.Y = 10;
                        Select3 = false;

                    }
                }
                break;

            default:
               break;
        } 

`

好像没问题。还有什么建议吗?

2 个答案:

答案 0 :(得分:0)

目前还不清楚你在这里做了什么,但在XNA中,MouseState是一个状态变量,而不是事件变量,所以鼠标.LeftButton == ButtonState.Pressed会在用户按住鼠标左键时保持为真。

如果您想在鼠标状态发生变化时触发某些内容,例如在downclick上,您需要记住以前的状态并将其与当前状态进行比较。有关更多详细信息,请参阅本文:XNA Mouse Input and Handling

当前,您检查是否选择了矩形的检查看起来很腥。这些检查不对称地处理X和Y,即使你没有为我们定义任何这些对象,这对我来说似乎是错误的。也许以下会更好用吗?

        if ((mouse.X >= drawRectangle.X && mouse.X <= drawRectangle.X + currentCharacter.Width
           && mouse.Y >= drawRectangle.Y && mouse.Y <= drawRectangle.Y + currentCharacter.Height)

你应该把它提取到一个合适的方法,BTW。

答案 1 :(得分:0)

虽然@dbc肯定回答了你的问题,但我觉得还有更多需要。

我建议您根据鼠标的X / Y坐标和字符宽度/高度每帧创建一个矩形。这样,您可以使用内置的Rectangle.Intersect或Rectangle.Contains方法。

还可以制作一个矩形列表(或者包含它们的任何内容。块,Npcs,w / e)。然后,创建一个返回所选模型的方法,如:

public Rectangle? GetSelected(List<Rectangle> squares)
{
    Rectangle mousePosition = new Rectangle(mouseX, mouseY, charWidth, charheight);

    foreach (var rect in squares)
    {
        if (rect.Contains(mousePosition))
        {
            return rect;
        }
    }

    return null;
}

然后检查GetSelected()返回的是否为null。如果是,则光标当前不会悬停在任何内容上。 但是,如果不是,你现在可以移动所说的Rectangle,因为你有一个引用它。

注意:由于Rectangle是一个结构,因此实际上不能使用矩形。并且结构不是通过引用转移,而是通过值转移。这意味着您从GetSelected()获取的矩形将只是您选择的方块的表示,而不是实际的方块。这意味着如果更改X,Y,Width或Height,则不会在列表中的Rectangles中更改。

这就是为什么我个人总是建议使用所有结构类型的包装,你已经连接了#34;任何事情。比如Vector2 for Position。这是一个游戏对象&#34; class,并始终引用整个类。这样,当您编辑Position的内容时,它仍将是正确的GameObject的位置,将会被更改。

有关更多信息,我强烈建议您同时阅读有关结构和课程之间差异的更多信息,并自己动手阅读它以了解其工作原理。