XNA Windows Phone 7 Sprite movement
- by Darren Gaughan
I'm working on a Windows phone game and I'm having difficulty with the sprite movement. What I want to do is make the sprite gradually move to the position that is touched on screen, when there is only one quick touch and release. At the minute all I can do is either make the sprite jump instantly to the touch location or move along to the touch location when the touch is held down.
Code for jumping to touch location:
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
if ((tl.State == TouchLocationState.Pressed)
|| (tl.State == TouchLocationState.Moved))
{
Vector2 newPos = new Vector2(tl.Position.X,tl.Position.Y);
if (position != newPos)
{
while (position.X < newPos.X)
{
position.X += (float)theGameTime.ElapsedGameTime.Milliseconds / 10.0f * spriteDirectionRight;
}
}
}
}
Code to gradually move along while touch is held:
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
if ((tl.State == TouchLocationState.Pressed)
|| (tl.State == TouchLocationState.Moved))
{
Vector2 newPos = new Vector2(tl.Position.X,tl.Position.Y);
if (position != newPos)
{
position.X += (float)theGameTime.ElapsedGameTime.Milliseconds / 10.0f * spriteDirectionRight;
}
}
}
These are in the Update() method of the Sprite class.