XNA中的角色跳跃

时间:2014-02-20 16:48:01

标签: c# xna

我是游戏编程的新手,但我对这个领域感兴趣;现在我正在为我的课程工作做一个小游戏。我利用互联网上的一些想法让我的英雄跳跃;代码正在运行,但在我按下第一时间空间后,英雄正在跳跃并且没有回到他的位置,他仍然在屏幕的顶部。请帮我制作我的英雄,然后回到他的初始位置。如果我再次按空间,他会跳跃,但是跳到屏幕顶部。

public void Initialize()
{
    startY = position.Y;
    jumping = false;
    jumpspeed = 0;
}

public void Update(GameTime gameTime)
{
    KeyboardState keyState = Keyboard.GetState();
    rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
    origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
    AnimateRight(gameTime);//calling AnimateRight function to animate heroes sprite
    if (jumping)
    {
        position.Y += jumpspeed;
        jumpspeed += 1;
        if (position.Y >= startY)
        {
            position.Y = startY;
            jumping = false;
        }
    }
    else
    {
        if (keyState.IsKeyDown(Keys.Space))
        {
            jumping = true;
            jumpspeed = -14;

        }
    }
}

2 个答案:

答案 0 :(得分:1)

Space 时必须设置startY

if (keyState.IsKeyDown(Keys.Space))
{
    jumping = true;
    jumpspeed = -14;
    startY = position.Y;
}

答案 1 :(得分:0)

我知道你是新人,但我复制了一个运动系统,如果你能理解它就会很好,here是看到玩家运动的链接,而here就是网络它的网站。如果您要下载here,则链接。

这个玩家运动使用了几个重要的东西,

首先:

在Player类中有一个名为public Player的方法,是的,你创建了一个与该类同名的方法。通过这样做,您可以将信息从Game1类转移到Player类。所以你可以发送玩家的纹理,位置,速度等等......

第二

在Player方法中,需要收集从Game1类调用的信息并将其存储在Player类中。因此,如果您想要传输播放器的纹理,则需要执行以下操作。

创建播放器纹理并创建一个链接,允许您创建指向Player类的链接:

Texture2D personTexture;

'播放器'

然后在加载内容中,您需要调用personTexture并将其放入播放器功能:

personTexture = Content.Load <Texture2D>("Person");

player = new Player(personTexture);

现在,Texture现在位于Player类中的Player方法的旁边,您现在将它存储在Player类中,以便您可以使用它,添加Texture2D Texture您的Player类然后输入以下内容:

public Player(Texture2D Texture) 
{
     this.Texture = Texture;//this.Texture is the one you create in side the 
     Player class, the other is the Texture you stated 
}

现在你完成了并且能够在那个类中使用你的纹理。

希望这有助于您了解如何创建跳跃播放器。

相关问题