play animation while keypressed

Jun 24, 2010 at 4:49 PM
Edited Jun 24, 2010 at 5:09 PM

My current code only plays my animation when the key is released, using key.Isdown. It freezes while the key is held down. As it is a walk animation, I need it to play while the key is being pressed, and finish as soon as it is released.

public void PlayAnimation3()
            int animationIndex = animationController.AnimationIndex("Walk");


            //begin playing the animation, looping
            animation = animationController.PlayAnimation(animationIndex);

which is called by,

 if (state.KeyboardState.KeyState.A.OnPressed)
                playerPositionAdd.X += 100;

Jun 27, 2010 at 1:17 PM

When you call PlayAnimation, it is creating a new instance of that animation. It's not resuming an existing animation. (So you can technically play the same animation twice at the same time).

If you have a looping 'walk' animation, I find a useful technique is to use animation weighting to blend the animation in and out.
Leave the animation playing continuously, and as your character is moving, increase the weighting. Use Zero when not moving (so the animation has no effect) and One when moving at full speed. Just be careful to make sure all your animations that are playing add up to a weighting of One (otherwise things can get weird :-)

Jun 28, 2010 at 8:05 PM
Edited Jun 28, 2010 at 8:08 PM

I've tried using weighting but I'm still getting the same problem, while the button is pressed, the animation freezes at the first frame, once released, it plays. My PlayAnimation() are in my actor class, and my input code is in my main Game class. I'm having trouble controlling the animations through my input class. Is what I'm doing now a good solution, or is there a better way to tie my animations to input?

Jun 29, 2010 at 10:00 PM
Edited Jun 29, 2010 at 10:09 PM

The problem is likely that you are calling Stop/Play animation every frame (while the key is pressed).
This will be causing the previous animation to stop, and a new one to start from zero (this sound be easy enough to check).
I suggested using weighting by leaving a single instance of the animation (looping) playing continuously. (ie, start the animation when you create the model). When it's weighting is zero it'll be essentially free so you don't have to worry about performance.