一一移动矩形 [英] moving rectangles one by one
问题描述
我在我的 XNA 程序中制作了几个矩形.下一步是选择它们并逐个移动它们.
I have made several rectangles in my XNA program. The next step is to choose them and move them one by one.
要选择它们,我使用代码
To choose them, I use the code
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;
}
}
然而,这里有些不太对劲.在我移动一个矩形之后,又想移动另一个.但我无法取消选择前一个矩形.请问我应该怎么改?
However, something is not quite right here. After I move one rectangle, and want to move another one. But I can not deselect the previous rectangle. What should I change it please?
更多更新.谢谢 dbc,我像这样修复我的代码
More update. Thanks dbc, I fix my code like this
我将代码修复为这样`bool Select1 = true;bool Select2 = true;bool Select3 = true;
I fix the code as this `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;
}
`
好像没问题.请问还有什么建议吗?
It seems ok. Any more suggestion please?
推荐答案
不清楚你在这里做什么,但在 XNA 中,MouseState 是 state 变量而不是 event 变量,所以鼠标.只要您的用户按住鼠标左键,LeftButton == ButtonState.Pressed 就会保持为真.
It's not clear what you are doing here, but in XNA, MouseState is a state variable not an event variable, so mouse.LeftButton == ButtonState.Pressed stays true as long your user is holding the left mouse button down.
如果你想在鼠标状态改变时触发某些东西,例如在向下单击时,您需要记住之前的状态并将其与当前状态进行比较.有关更多详细信息,请参阅例如这篇文章:XNA 鼠标输入和处理.
If you want to trigger something when the mouse state changes, e.g. on a downclick, you need to remember the previous state and compare it with the current state. For more details, see for instance this article: XNA Mouse Input and Handling.
顺便说一句,您检查是否选择了矩形看起来很可疑.检查不对称地处理 X 和 Y,这对我来说似乎是错误的,即使您没有为我们定义任何这些对象.也许以下方法会更好?
Incidcentally, your check(s) to see if a rectangle is selected look fishy. The checks treat X and Y asymmetrically, which seems wrong to me even though you did not define any of these objects for us. Maybe the following would work better?
if ((mouse.X >= drawRectangle.X && mouse.X <= drawRectangle.X + currentCharacter.Width
&& mouse.Y >= drawRectangle.Y && mouse.Y <= drawRectangle.Y + currentCharacter.Height)
顺便说一句,您应该将其提取为适当的方法.
You should extract this into a proper method, BTW.
这篇关于一一移动矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!