tags:

views:

28

answers:

1

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 (currentImageIndex > 7)
                        currentImageIndex = 0;
                }
            if (aCurrentKeyboardState.IsKeyDown(Keys.None))
                mCurrentState = State.Idle;
        }
    }
+1  A: 

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.

Heap
Thank you get much. I had actually done this before and taken it away due to a looping problem in one of my attack methods. However, I was reminded of something I forgot about. Thanks again!
Dave