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;
}
In order to move them, I use
//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?
More update. Thanks dbc, I fix my code like this
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?
See Question&Answers more detail:os