Walking to Idle animation transition trouble
I'm having trouble transitioning my walking to idle animations.
Maybe there's a better way to go about this?
The problem: I can check for NO key presses on the keyboard, but it causes the walking animation to display only the first frame and not the full animation.
The question: How can I change this so that when the user is done walking, it will change the state back to "Idle" without conflicting with the animations of walking left and right.
private void UpdateMovement(KeyboardState aCurrentKeyboardState, GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; //Framerate control
if (timeSinceLastFrame > millisecondsPerFrame) //Framerate control
{
timeSinceLastFrame -= millisecondsPerFrame; //Framerate control
//Idle if no keys are down
if (mCurrentState == State.Idle)
{
Position.Y = 210;
currentImageIndex++;
if (currentImageIndex < 17 || currentImageIndex > 23)
currentImageIndex = 17;
}
//Walk Left
if (aCurrentKeyboardState.IsKeyDown(Keys.Left))
{
mCurrentState = State.Walking;
if (currentImageIndex < 8 || currentImageIndex > 15)
currentImageIndex = 8;
Position.Y = 200;
currentImageIndex++;
Position.X += MOVE_LEFT;
if (currentImageIndex > 15)
currentImageIndex = 8;
}
//Walk Right
if (aCurrentKeyboardState.IsKeyDown(Keys.Right))
{
mCurrentState = State.Walking;
if (currentImageIndex > 7)
currentImageIndex = 0;
Position.Y = 200; ;
currentImageIndex++;
Position.X += MOVE_RIGHT;
if (currentImageIn开发者_高级运维dex > 7)
currentImageIndex = 0;
}
if (aCurrentKeyboardState.IsKeyDown(Keys.None))
mCurrentState = State.Idle;
}
}
Using Keys.None
is not semantically equivalent to "no keys are pressed", it is rather just a reserved value by the operating system. See for instance the documentation for the Keys enumeration. Therefore, I doubt this code will lead your character back to State.Idle
after having reached State.Walking
, the animation will just stop when you release the keys. To check whether no keys are pressed alter the last if
statement to use aCurrentKeyboardState.GetPressedKeys().Length == 0
instead, or swap with an else
statement which then would be reached when neither of Keys.Left
or Keys.Right
are pressed.
精彩评论