如何在返回步行状态之前先在Update_Hit函数中完成精灵动画?

时间:2013-02-25 13:49:51

标签: xna xna-4.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;
        }
    }

如何在将状态更改为步行之前完成点击动画?

1 个答案:

答案 0 :(得分: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);

}