简体   繁体   English

XNA对角运动钥匙释放

[英]XNA Diagonal Movement Key Release

Ok, so say you're playing a top down game. 好的,假设您正在玩自上而下的游戏。 You press W and D to go in the up-right direction. 您按WD向上移动。 Now you just want to go right, so you release the W key. 现在,您只想右移,因此释放W键。 One would expect you to then change directions and head right, but instead, you continue up-right. 人们会期望您随后改变方向并向右行驶,但相反,您将继续直立。 What can I do to solve this problem? 我该怎么做才能解决这个问题? Here is my code: 这是我的代码:

        if (kb.IsKeyDown(Keys.W) || kb.IsKeyDown(Keys.A) || kb.IsKeyDown(Keys.S) || kb.IsKeyDown(Keys.D))
        {
            if (kb.IsKeyDown(Keys.W))
                velocity.Y = -movespeed;
            else if (kb.IsKeyDown(Keys.A))
                velocity.X = -movespeed;
            else if (kb.IsKeyDown(Keys.S))
                velocity.Y = movespeed;
            else if (kb.IsKeyDown(Keys.D))
                velocity.X = movespeed;
            else if (kb.IsKeyDown(Keys.W) && kb.IsKeyDown(Keys.D))
            {
                velocity.X = movespeed;
                velocity.Y = -movespeed;
            }
            else if (kb.IsKeyDown(Keys.W) && kb.IsKeyDown(Keys.A))
            {
                velocity.X = -movespeed;
                velocity.Y = -movespeed;
            }
            else if (kb.IsKeyDown(Keys.S) && kb.IsKeyDown(Keys.D))
            {
                velocity.X = movespeed;
                velocity.Y = movespeed;
            }
            else if (kb.IsKeyDown(Keys.S) && kb.IsKeyDown(Keys.A))
            {
                velocity.X = -movespeed;
                velocity.Y = movespeed;
            }
        }
        else
            velocity *= .9f;

        position += velocity;

So I figured out the answer myself after a few hours of struggle. 因此,经过几个小时的努力,我自己找出了答案。

Simply setting the velocity to zero before the checks worked: EDIT: multiplying it by .89 actually made for much smoother movement. 只需在检查工作之前将速度设置为零即可:编辑:将速度乘以.89实际上可以使运动更加平滑。

        if (kb.IsKeyDown(Keys.W) || kb.IsKeyDown(Keys.A) || kb.IsKeyDown(Keys.S) || kb.IsKeyDown(Keys.D))
        {
            velocity *= .89f
            if (kb.IsKeyDown(Keys.W))
                velocity.Y = -movespeed;
            if (kb.IsKeyDown(Keys.A))
                velocity.X = -movespeed;
            if (kb.IsKeyDown(Keys.S))
                velocity.Y = movespeed;
            if (kb.IsKeyDown(Keys.D))
                velocity.X = movespeed;
        }
        else
            velocity *= .9f;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM