0

这是我用于切换状态的代码:

    if (mCurrentState == State.Walking)
    {
        action = "stand";
        Update_Stand(gameTime);

        if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
        {
            action = "run";
            feetPosition.X += MOVE_LEFT;
            effect = SpriteEffects.None;

            Update_Run(gameTime);

        }
        else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
        {
            action = "run";
            feetPosition.X += MOVE_RIGHT;
            effect = SpriteEffects.FlipHorizontally;

            Update_Run(gameTime);
        }

        if (aCurrentKeyboardState.IsKeyDown(Keys.Z) == true)
        {
            mCurrentState = State.Hitting;
        }
    }

    if (mCurrentState == State.Hitting)
    {
        action = "hit";

        Update_Hit(gameTime);

        mCurrentState = State.Walking;
    }

我的 Update_Hit(GameTime gameTime) 方法就是这样。我有 2 个精灵要制作动画。

public void Update_Hit(GameTime gameTime)
    {
        // Check if it is a time to progress to the next frame
        if (nextFrame_Hit >= frameInterval_Hit)
        {
            // Progress to the next frame in the row
            currentFrame_Hit.X++;

            // If reached end of the row advance to the next row, reset to the first frame 
            if (currentFrame_Hit.X >= sheetSize_Hit.X)
            {
                currentFrame_Hit.X = 0;
                currentFrame_Hit.Y++;
            }

            // If reached last row in the frame sheet, jump to the first row again
            if (currentFrame_Hit.Y >= sheetSize_Hit.Y)
                currentFrame_Hit.Y = 0;

            // Reset time interval for next frame
            nextFrame_Hit = TimeSpan.Zero;
        }
        else
        {
            // Wait for the next frame
            nextFrame_Hit += gameTime.ElapsedGameTime;
        }
    }

如何在将状态更改回行走之前完成命中动画?

4

1 回答 1

0

由于您使用的是固定的游戏时间,因此您需要确保在动画完成之前没有更改状态,以确保在每次更新时您都会获得动画的单帧,直到动画完成:

if (nextFrame_Hit >= frameInterval_Hit)
{
    // Progress to the next frame in the row
    currentFrame_Hit.X++;

    // If reached end of the row advance to the next row, reset to the first frame 
    if (currentFrame_Hit.X >= sheetSize_Hit.X)
    {
        currentFrame_Hit.X = 0;
        currentFrame_Hit.Y++;
    }

    // If reached last row in the frame sheet, jump to the first row again
    if (currentFrame_Hit.Y >= sheetSize_Hit.Y)
    {
        currentFrame_Hit.Y = 0;
        mCurrentState = State.Walking;
    }

    // Reset time interval for next frame
    nextFrame_Hit = TimeSpan.Zero;
}
else
{
    // Wait for the next frame
    nextFrame_Hit += gameTime.ElapsedGameTime;
}

你需要改变这个:

if (mCurrentState == State.Hitting)
{
    action = "hit";

    Update_Hit(gameTime);

}
于 2013-02-25T14:21:10.173 回答