Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 斜移雪碧_C#_Monogame - Fatal编程技术网

C# 斜移雪碧

C# 斜移雪碧,c#,monogame,C#,Monogame,所以,当我在键盘上按SWD键时,精灵会移动,但它不想变成对角线,就像一次只能按一个按钮一样。这是我的代码 我想让它做的是,当你说,例如,按w和d,它向东北方向移动。其他钥匙也一样 class Player { Texture2D playertexture; Vector2 playerposition; public Player(Vector2 playerposition, Texture2D playertexture) { this

所以,当我在键盘上按SWD键时,精灵会移动,但它不想变成对角线,就像一次只能按一个按钮一样。这是我的代码

我想让它做的是,当你说,例如,按w和d,它向东北方向移动。其他钥匙也一样

class Player
{
    Texture2D playertexture;
    Vector2 playerposition;



    public Player(Vector2 playerposition, Texture2D playertexture)
    {
        this.playertexture = playertexture;
        this.playerposition = playerposition;
    }

    public void Update(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.D)) //right
        {
            playerposition.X += 1;

        }
        else if (Keyboard.GetState().IsKeyDown(Keys.A)) //left
        {
            playerposition.X -= 1;

        }
        else if (Keyboard.GetState().IsKeyDown(Keys.W)) //up
        {
            playerposition.Y -= 1;

        }
        else if (Keyboard.GetState().IsKeyDown(Keys.S)) //down
        {
            playerposition.Y += 1;

        }

    }
    public void Draw(SpriteBatch spriteBatch)
    {

        spriteBatch.Draw(playertexture, playerposition, Color.White);


    }
}

您可以轻松地将所有按下的键相加,得到范围为-1,-1到1,1的“最终”运动矢量

    public Vector2 GetPlayerMovement()
    {
        Vector2 r = Vector2.Zero;
        KeyboardState ks = Keyboard.GetState();

        if (ks.IsKeyDown(MOVEUP))
            r.Y -= 1;
        if (ks.IsKeyDown(MOVEDOWN))
            r.Y += 1;
        if (ks.IsKeyDown(MOVELEFT))
            r.X -= 1;
        if (ks.IsKeyDown(MOVERIGHT))
            r.X += 1;

        return r;
    }

MOVEUP/DOWN/LEFT/RIGHT表示键WSAD

,这是因为您使用
else if
排除了其他按钮。每次按钮检查都使用独立的
if
。我太傻了,谢谢你帮我,天哪,哈哈哈,我已经为此紧张了一个小时了哈哈哈,非常感谢