We want to now make the dwarf stand up by running the animation backwards, but there is a subtle problem. The animation is tagged as "IsLooping == false", but we are at the end of the animation, so the ElapsedTime is equal to the duration. To override this, we will manually step through the animation.
- To manually step through the animation, we have to first make the Update method pass the gameTime variable to the UpdateState method:
private void UpdateState(GameTime gameTime)
- 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":
if (keyState.IsKeyDown(Keys.Space))
{
crouchDown.ElapsedTime = crouchDown.AnimationSource.Duration;
crouchDown.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:
else if (state == "standUp")
{
if (crouchDown.ElapsedTime - gameTime.ElapsedGameTime.Ticks <= 0)
{
crouchDown.SpeedFactor = 1;
crouchDown.ElapsedTime = 0;
idle.ElapsedTime = 0;
state = "idle";
}
else
crouchDown.ElapsedTime -= gameTime.ElapsedGameTime.Ticks;
foreach (BonePose p in poses)
{
p.CurrentAnimation = crouchDown;
p.CurrentBlendAnimation = null;
}
}