How to slow down or stop key presses in XNA

后端 未结 12 2090
北恋
北恋 2021-02-01 05:38

I\'ve begun writing a game using XNA Framework and have hit some simple problem I do not know how to solve correctly.

I\'m displaying a menu using Texture2D and using th

12条回答
  •  时光说笑
    2021-02-01 06:06

    Ok, I've figured it out. First, I added a

    private Keys keyPressed = Keys.None;
    

    and in my Update() method, I do the following:

     KeyboardState keyboardState = Keyboard.GetState();
    
    if (keyboardState.IsKeyUp(keyPressed))
    {
        keyPressed = Keys.None;
    }
    
    if (keyboardState.IsKeyDown(keyPressed))
    {
        return;
    }
    
    // Some additionnal stuff is done according to direction
    if (keyboardState.IsKeyDown(Keys.Up))
    {
        keyPressed = Keys.Up;
    }
    else if (keyboardState.IsKeyDown(Keys.Down))
    {
        keyPressed = Keys.Down;
    }
    

    It seems to be working correctly.

提交回复
热议问题