0

友達と私は自分たちでゲームを作ることにしました。それは、ポケモンと古い牧場物語のグラフィック スタイルを使用しています。私はアニメーションでいくつかのテストを行っていましたが、動作するようになりました。私のプレーヤーのスプライト シートには 8 つの画像があります (各方向に 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);
    }
}
4

2 に答える 2

0

私はこれがちょっと古いことを知っていますが、このシナリオでは、2 つのキーが同時に押された場合にプレイヤーが移動する方向に影響を与える、プレイヤーが制御できない順序の優先順位があります。より良い解決策は次のとおりです。

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);
}

このように、入力とロジックを分離することで、物事がより明確になり、特定の入力に優先順位を付けません (上/下については例外... しかし、w/e) プレーヤーが左と右に移動しようとすると同時に、移動はキャンセルされます。

于 2014-06-25T19:02:10.937 に答える
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;

一度に 2 方向に移動する場合は、次のようにします。

    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;
于 2013-04-12T15:16:37.620 に答える