逐步向上移动精灵(跳转)

时间:2014-02-19 06:29:15

标签: c# xna windows-phone

当我触摸屏幕时,我在使用精灵跳转到我的XNA Game for Windows Phone时出现问题。

目前我有以下代码。

foreach (TouchLocation tl in touchCollection)
{
    if ((tl.State == TouchLocationState.Pressed))
    {
        rectangleSprite.Y -= 55;
     }
}

注:

rectangleSprite = new Rectangle((int)newPosition.X, (int)newPosition.Y, textureSprite.Width, textureSprite.Height)

我的代码使精灵移动到新位置。这可以作为一个跳跃,但我希望它更加渐进,所以你可以看到它跳跃。

1 个答案:

答案 0 :(得分:3)

为了让你的角色更好地跳跃,你必须为你的代码添加速度,并将你的跳跃功能放在update()方法中。

您需要添加重力。 在您的更新中,您可以添加类似的内容。

float i = 1; velocity.Y += 0.15f * i;

提醒:^这仅在您设置代码时才有效,您的角色不能低于它所代表的曲面。 因为每次代码调用update()方法时它都会降低。

然后,如果你愿意,可以制作一个boolean来检查你的玩家是否跳过了。

foreach (TouchLocation tl in touchCollection)
{
    if ((tl.State == TouchLocationState.Pressed && hasJumped == false))
    {
        position.Y -=10f;
        veloctity.Y -= 5f;
     }
}

修改

不要忘记更新角色的位置。 职位:

Vector2 position;

更新代码:

position = position + velocity;

您可能想要阅读/观看的一些内容:

喜欢 scheien 说:
http://www.xnadevelopment.com/tutorials/thewizardjumping/thewizardjumping.shtml

可能有助于解释的小视频。
http://www.youtube.com/watch?v=ZLxIShw-7ac

我也在本网站上学习,欢迎任何建议或编辑!