0

在我的精灵跳跃时,我需要制作动画。如果按下某个键,我尝试加载动画但它不起作用。我按键跳跃Space。所有其他动画功能。所以,我的问题是通过按键加载动画。我想我可以使用GameTime.Elapsed.TotalMilliseconds,但我不确定。反正我是新手,这是我的第一场比赛。

class Player
{
    PlayerAnimation animation;
    Animation walk;
    Animation stay;
    Animation jump;;
    public Vector2 position;
    public Vector2 velocity;
    public Rectangle rect;
    public bool jumping = false;
    public int health = 5;
    public Vector2 Position{ get { return position; } }


    public Player() { }

    public void Load(ContentManager Content) {

        walk = new Animation(Content.Load<Texture2D>("walk"), 46, 0.1f, true, rect);
        animazioneMinima = new Animation(Content.Load<Texture2D>("stay"),37, 0.15f, true, rect);
        jump = new Animation(Content.Load<Texture2D>("jump"), 51, 0.1f, true , rect);


    }

    public void Update(GameTime gameTime)
    {
        position += velocity;
        rect = new Rectangle((int)position.X, (int)position.Y, 43, 46);
        Input(gameTime);



        if (velocity.X != 0)
            PlayerAnimation.PlayAnimation(walk);

        else if (velocity.X == 0)
            PlayerAnimation.PlayAnimation(stay;
        if (velocity.Y < 10)
            velocity.Y += 0.4f;
    }


    private void Input(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.D))
            velocity.X = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 3;
        else if (Keyboard.GetState().IsKeyDown(Keys.A))
            velocity.X = -(float)gameTime.ElapsedGameTime.TotalMilliseconds / 3;
        else velocity.X = 0f;


        if (Keyboard.GetState().IsKeyDown(Keys.Space) && jumping == false)
        {
            position.Y -= 5f;
            velocity.Y = -9f;
            jumping = true;
        }

    }


    public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        SpriteEffects rotate = SpriteEffects.None;

        if (velocity.X >= 0)
            rotate = SpriteEffects.None;
        else if (velocity.X < 0)
            rotate = SpriteEffects.FlipHorizontally;

        PlayerAnimation.Draw(gameTime, spriteBatch, position, rotate);
    }
  }
}

我应该做什么?:D

4

1 回答 1

0

我不确切知道您的代码是如何工作的,但也许 yau 可以编写如下内容:

if (velocity.Y != 0)
{ 
PlayerAnimation.PlayAnimation(jump); 
velocity.Y -= 0.05f
}

并安装:

if (Keyboard.GetState().IsKeyDown(Keys.Space) && jumping == false)` 

你可以写:

if (Keyboard.GetState().IsKeyDown(Keys.Space) && velocity.Y == 0)

那么你可以完全跳过使用跳转布尔。

于 2013-10-24T11:13:08.517 回答