走向空闲动画过渡的麻烦

时间:2010-07-12 20:19:55

标签: c# xna

我无法将步行转换为空闲动画。

也许还有更好的方法可以解决这个问题?

问题:我可以检查键盘上没有按键,但它会导致步行动画仅显示第一帧而不是完整动画。

问题:如何更改此设置,以便当用户完成步行时,它会将状态更改回“空闲”状态,而不会与左右行走的动画冲突。

   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 个答案:

答案 0 :(得分:1)

使用Keys.None在语义上并不等同于“没有按下任何键”,它只是操作系统保留的值。例如,参见documentation for the Keys enumeration。因此,我怀疑此代码会在到达State.Idle后将您的角色引回State.Walking,动画将在您释放按键时停止。要检查是否没有按下任何键,请改变上一个if语句以使用aCurrentKeyboardState.GetPressedKeys().Length == 0,或者使用else语句进行交换,当Keys.LeftKeys.Right时都不会出现该语句{1}}被按下了。