Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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>
}
share|improve this question

closed as too localized by Byte56, Sean Middleditch, bummzack, Jimmy Shelter, ashes999 Feb 28 '13 at 14:35

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

2 Answers 2

up vote 2 down vote accepted

Your description is a bit vague, and you pasted way too much code... but I think the problem lies in the Update_Animation method:

You increase the value of nextFrame multiple times in an update if your character is running or 'hitting'.

Either make sure you call the Update_Animation method only once in an update, or just don't use the same variable (nextFrame) for all calls.

share|improve this answer
add comment

It seems like your Update_Animation method is called every Update or Draw call. That means that your animation speed depends on your computer's speed. On slow computers, animation is slow; on fast computers, animation is slow.

What you probably want with XNA is the game (and as much as possible, the UI) to run independently of the actual speed the game is executed.

What this boils down to (and the way I solved this in the past -- for animation, velocity/physics, etc.) is to update on a certain interval, i.e.

if ((DateTime.Now - lastFrameUpdate).TotalSeconds <= updateInterval) {
  Update_Animation(...)
  lastFrameUpdate = DateTime.Now;
}

In my case, I used a configurable interval like TimeSpan.FromSeconds(0.1).TotalSeconds to indicate frames should be updated every tenth of a second.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.