XNA Windows Phone 7 Sprite movement
Posted
by
Darren Gaughan
on Game Development
See other posts from Game Development
or by Darren Gaughan
Published on 2011-11-19T22:22:29Z
Indexed on
2011/11/20
2:17 UTC
Read the original article
Hit count: 254
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.
© Game Development or respective owner