I don't know why each time I call the Update_Animation function, my sprite animation runs faster. Is it caused by gameTime? How to fix it?
Here's the relevant code:
class Character
{
// <SNIP>
public void Update_Animation(Point sheetSize, TimeSpan frameInterval, GameTime gameTime)
{
if (nextFrame >= frameInterval)
{
currentFrame.X++;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
currentFrame.Y++;
}
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
nextFrame = TimeSpan.Zero;
}
else
{
nextFrame += gameTime.ElapsedGameTime;
}
}
// <SNIP>
private void UpdateMovement(KeyboardState aCurrentKeyboardState, GameTime gameTime)
{
if (mCurrentState == State.Walking)
{
action = "stand";
Update_Animation(sheetSize_Stand, frameInterval_Stand, gameTime);
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) && !aCurrentKeyboardState.IsKeyDown(Keys.Right))
{
action = "run";
effect = SpriteEffects.None;
mSpeed.X = CHARACTER_SPEED;
mDirection.X = MOVE_LEFT;
Update_Animation(sheetSize_Run, frameInterval_Run, gameTime);
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) && !aCurrentKeyboardState.IsKeyDown(Keys.Left))
{
action = "run";
effect = SpriteEffects.FlipHorizontally;
mSpeed.X = CHARACTER_SPEED;
mDirection.X = MOVE_RIGHT;
Update_Animation(sheetSize_Run, frameInterval_Run, gameTime);
}
if (aCurrentKeyboardState.IsKeyDown(Keys.Z) && mPreviousKeyboardState.IsKeyUp(Keys.Z))
{
mCurrentState = State.Hitting;
}
}
if (mCurrentState == State.Hitting)
{
action = "hit";
Update_Animation(sheetSize_Hit, frameInterval_Hit, gameTime);
mCurrentState = State.Walking;
}
}
// <SNIP>
}