【发布时间】:2011-11-20 11:04:30
【问题描述】:
我正在开发一款 Windows 手机游戏,但我在精灵移动方面遇到了困难。 我想要做的是让精灵逐渐移动到屏幕上触摸的位置,当只有一个快速触摸和释放时。 在这一刻,我所能做的就是让精灵立即跳转到触摸位置,或者在按住触摸时移动到触摸位置。
跳转到触摸位置的代码:
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;
}
}
}
}
按住触摸时逐渐移动的代码:
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;
}
}
}
这些在 Sprite 类的 Update() 方法中。
【问题讨论】:
标签: xna 2d sprite windows-phone-7.1