Reputation: 31
I've got an app where I drag a spaceship around the screen avoiding asteroids, and want the ship to return to it's original position once their rectangles collide, but it's not happening.
I'm unsure what's wrong...
Code:
//Drag Ship
TouchCollection touchLocations = TouchPanel.GetState();
foreach (TouchLocation touchLocation in touchLocations)
{
Rectangle touchRect = new Rectangle
((int)touchLocation.Position.X, (int)touchLocation.Position.Y, shipRect.Width, shipRect.Height);
if (touchLocation.State == TouchLocationState.Pressed
&& shipRect.Intersects(touchRect))
{
shipPressed = true;
}
else if (touchLocation.State == TouchLocationState.Moved && shipPressed)
{
shipRect.X = touchRect.X - touchRect.Width / 2;
shipRect.Y = touchRect.Y - touchRect.Height / 2;
}
else if (touchLocation.State == TouchLocationState.Released)
{
shipPressed = false;
}
else if (lnBtnPlay.Tapped == true)
{
}
}
Code 2:
if (shipRect.Intersects(asteroid1Rect))
{
shipPosition = new Vector2(10, 400);
}
Upvotes: 1
Views: 60
Reputation:
shipPressed
never gets set to false unless you actually release your finger. Therefore, even though you may be intersecting with the asteroid, the ship is constantly going back to your touch.
In general always remember to reset some things upon "death". Clear shipPressed
back to false
when you collide.
If the ship still isn't returning even if your finger is not touching the screen, then your shipPosition
is not synced up with your shipRect
. Make sure you keep both of these in sync always. Update them together always, or more succinctly, keep just the position, and store a width and height as well; you can freshly generate a rectangle from this easily with a property / method call.
Upvotes: 1