I want to run a animation until it has gone through all the frames, even when the activation key is no longer held. After the animation is completely finished, it goes back to walking state. I also want to force the animation to finish first before the player can trigger the new animation.
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;
}
My Update_Hit(GameTime gameTime) method is something like that.
// Amount of time between frames
TimeSpan frameInterval_Hit;
// Time passed since last frame
TimeSpan nextFrame_Hit;
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;
}
}