How to slow down or stop key presses in XNA

后端 未结 12 2556
北恋
北恋 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:10

    the best way to implement this is to cache the keyboard/gamepad state from the update statement that just passed.

    KeyboardState oldState;
    ...
    
    var newState = Keyboard.GetState();
    
    if (newState.IsKeyDown(Keys.Down) && !oldState.IsKeyDown(Keys.Down))
    {
        // the player just pressed down
    }
    else if (newState.IsKeyDown(Keys.Down) && oldState.IsKeyDown(Keys.Down))
    {
        // the player is holding the key down
    }
    else if (!newState.IsKeyDown(Keys.Down) && oldState.IsKeyDown(Keys.Down))
    {
        // the player was holding the key down, but has just let it go
    }
    
    oldState = newState;
    

    In your case, you probably only want to move "down" in the first case above, when the key was just pressed.

提交回复
热议问题