【问题标题】:XNA Diagonal Movement Key ReleaseXNA 对角移动钥匙释放
【发布时间】:2015-05-03 21:48:07
【问题描述】:

好的,假设您正在玩自上而下的游戏。您按WD 以朝右方向移动。现在你只想向右走,所以你释放W 键。人们会期望您随后改变方向并向右行驶,但相反,您会继续直立。我能做些什么来解决这个问题? 这是我的代码:

        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;

【问题讨论】:

    标签: c# xna game-physics


    【解决方案1】:

    所以经过几个小时的努力,我自己想出了答案。

    只需在检查生效之前将速度设置为零: 编辑:将它乘以 0.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;
    

    【讨论】:

    • 你有没有考虑过对角线速度比上/下/左/右移动更快?一种解决方案是将速度归一化,然后乘以速度。另一个是如果移动是对角线,则将速度除以sqrt(2)
    • 不,我没有。你能详细说明一下吗?
    • 对角线速度将为speed magnitude * sqrt(2)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2022-01-20
    相关资源
    最近更新 更多