We want to now make the dwarf stand up by running the animation backwards, but we can't use the AnimationEnded event because that only fires when we reach the end of animation and not when we reach the beginning.

  • In the "stayCrouched" state, If the user presses space, we want to start standing the dwarf back up. We need to set the SpeedFactor to 0 to disable the automatic animation stepping and change the state to "standUp":
    // Add this to the if (state == "stayCrouched") block in the UpdateState method
    if (keyState.IsKeyDown(Keys.Space))
    {
        crouch.ElapsedTime = crouch.AnimationSource.Duration;
        crouch.SpeedFactor = 0;
        state = "standUp";
    }
  • Now, we need to make the stand up state. If subtracting the update time from the animations elapsed time gives us a negative value, we will end the transition and go back to the idle state. Otherwise, we subtract the elapsed game time from the elapsed animation time. This requires a little bit of careful coding:
    // Add this to the UpdateState method
    else if (state == "standUp")
    {
        if (crouch.ElapsedTime - gameTime.ElapsedGameTime.Ticks <= 0)
        {
            crouch.SpeedFactor = 1;
            crouch.ElapsedTime = 0;
            idle.ElapsedTime = 0;
            state = "idle";
        }
        else
            crouch.ElapsedTime -= gameTime.ElapsedGameTime.Ticks;
        RunController(dwarfAnimator, crouch);
    }

Last edited Mar 7, 2007 at 8:50 PM by dastle, version 4

Comments

No comments yet.