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
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.