XNA / C#2D播放器动画问题

问题描述 投票:0回答:2

我的朋友和我决定为自己制作一个游戏。这是一个口袋妖怪和旧的Harvest Moon图形样式。我正在做一些动画测试,我让它工作。我的播放器Sprite Sheet有八个图像(每个方向2个)。

但是,当我按住向上箭头键和向左或向右或向下箭头键以及向左或向右键时,它会尝试同时执行两个动画。我知道必须有办法解决这个问题我只需要有人告诉我如何解决这个问题。

这是我的播放器的动画类:

public class Animation
{
    Texture2D texture;
    Rectangle rectangle;
    public Vector2 position;
    Vector2 origin;
    Vector2 velocity;

    int currentFrame;
    int frameHeight;
    int frameWidth;

    float timer;
    float interval = 75;

    public Animation(Texture2D newTexture, Vector2 newPosition, int newFrameHeight, int newFrameWidth)
    {
        texture = newTexture;
        position = newPosition;
        frameWidth = newFrameWidth;
        frameHeight = newFrameHeight;
    }

    public void Update(GameTime gameTime)
    {
        rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
        origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
        position = position + velocity;

        // Comment Out For Camera!!!!
        if (position.X <= 0 + 10) position.X = 0 + 10;
        if (position.X >= 1920 - rectangle.Width + 5) position.X = 1920 - rectangle.Width + 5;
        if (position.Y <= 0 + 10) position.Y = 0 + 10;
        if (position.Y >= 1080 - rectangle.Height + 7) position.Y = 1080 - rectangle.Height + 7;

        if (Keyboard.GetState().IsKeyDown(Keys.Right))
        {
            AnimateRight(gameTime);
            velocity.X = 2;
        }
        else if (Keyboard.GetState().IsKeyDown(Keys.Left))
        {
            AnimateLeft(gameTime);
            velocity.X = -2;
        }
        else velocity = Vector2.Zero;

        if (Keyboard.GetState().IsKeyDown(Keys.Up))
        {
            AnimateUp(gameTime);
            velocity.Y = -2;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.Down))
        {
            AnimateDown(gameTime);
            velocity.Y = 2;
        }
    }

    public void AnimateRight(GameTime gameTime)
    {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 1)
                    currentFrame = 0;
            }
    }

    public void AnimateLeft(GameTime gameTime)
    {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 3 || currentFrame < 2)
                    currentFrame = 2;
            }
    }

    public void AnimateUp(GameTime gameTime)
    {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 5 || currentFrame < 4)
                    currentFrame = 4;
            }
    }

    public void AnimateDown(GameTime gameTime)
    {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 7 || currentFrame < 6)
                    currentFrame = 6;
            }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
    }
}
c# animation 2d xna-4.0
2个回答
0
投票

这将允许一次只在一个方向上移动(我相信如果你遵循口袋妖怪的游戏风格你想要的):

    if (Keyboard.GetState().IsKeyDown(Keys.Right))
    {
        AnimateRight(gameTime);
        velocity.X = 2;
    }
    else if (Keyboard.GetState().IsKeyDown(Keys.Left))
    {
        AnimateLeft(gameTime);
        velocity.X = -2;
    }       
    else if (Keyboard.GetState().IsKeyDown(Keys.Up))
    {
        AnimateUp(gameTime);
        velocity.Y = -2;
    }
    else if (Keyboard.GetState().IsKeyDown(Keys.Down))
    {
        AnimateDown(gameTime);
        velocity.Y = 2;
    }
    else velocity = Vector2.Zero;

如果您想一次移动两个方向,例如:

    bool animated = false;
    if (Keyboard.GetState().IsKeyDown(Keys.Right))
    {
        AnimateRight(gameTime);
        velocity.X = 2;
        animated = true;
    }
    else if (Keyboard.GetState().IsKeyDown(Keys.Left))
    {
        AnimateLeft(gameTime);
        velocity.X = -2;
        animated = true;
    }    
    else
    {
         velocity.X = 0;
    }  

    if (Keyboard.GetState().IsKeyDown(Keys.Up))
    {
        if(animated == false)
        {   AnimateUp(gameTime); }

        velocity.Y = -2;
    }
    else if (Keyboard.GetState().IsKeyDown(Keys.Down))
    {
        if(animated == false)
        { AnimateDown(gameTime); }
        velocity.Y = 2;
    }
    else velocity.Y = 0;

0
投票

我知道这有点旧了但是在这种情况下,如果同时按下两个键,则播放器无法控制哪个顺序优先级影响他将移动的方向。更好的解决方案是:

velocity = Vector2.Zero;

if (Keyboard.GetState().IsKeyDown(Keys.Right))
    velocity.X += 2;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
    velocity.X -= 2;

if (Keyboard.GetState().IsKeyDown(Keys.Up))
    velocity.Y -= 2;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
    velocity.Y += 2;

if (velocity.X > 0)
    AnimateRight(gameTime);
else if (velocity.X < 0)
    AnimateLeft(gameTime);

// Animate Up/Down only if Left/Right does not... 
// not sure if needed but will follow the style.
if (velocity.X == 0) 
{
    if (velocity.Y > 0)
        AnimateDown(gameTime);
    else if (velocity.Y < 0)
        AnimateUp(gameTime);
}

这样,我们将输入和逻辑分开,这使得事情更加清晰,我们不会优先于某些输入(xept用于向上/向下......但是w / e)如果玩家试图左右移动与此同时,该运动将被取消。

© www.soinside.com 2019 - 2024. All rights reserved.