0
\$\begingroup\$

Why hello there, im developing a 2D game using XNA. My character movment is currently based on the keyboard state. Example (in the right order):

if (state.IsKeyDown(Keys.W))
    {
          //Do stuff
    }
else if (state.IsKeyDown(Keys.D))
    {
          //Do Stuff
    }               
else if (state.IsKeyDown(Keys.A))
    {
          //Do Stuff
    }
else if (state.IsKeyDown(Keys.S))
    {
          //Do Stuff
    }

That way I am able to deny diagonal movment. So my problem is, if "W" is pressed, meaning the character is moving up, And a frame after that while W is still pressed and I press "S" for example, It will keep moving up. I've been looking for a solution for a while and didn't come up with anything good enough. What I need help with is ideas about how to detect if a different key was pressed after W (because it is the first one to check, it doesn't check the others), while denying diagonal movment. Any help would be sincerely appreciated, Thanks in advance.

\$\endgroup\$
2
\$\begingroup\$

I believe you want something like the below. It will keep track of the most recently pressed key in the variable _movementKey and set the movement key to none when the most recent key is released.

So the end result should be _movementKey contains the key that dictates direction, it will always be most recently pressed key and will be reset to Key.None when the most recent key is released.

// ###################### 
// non local variables 
// ######################

private KeyboardState _oldState;
private Key _movementKey; //This keeps track of the key that was pressed last.
                          //It is reset to Key.None id released no matter what
                          //other keys are pressed.

// ######################
// function to update input
// ######################

var newState = Keyboard.GetState();

if (!newState.IsKeyDown(_movementKey) && _oldState.IsKeyDown(_movementKey)) 
{
    //If the current key that dictates the moment was released (down last 
    //frame and up this frame) then set the moment key to none. 
    _movementKey = Key.None;
} else {
    //Else use this if else chain to detect when a button is pressed (definition 
    //of pressed being it was up last frame and is down this frame). When it 
    //detects a button being pushed it sets the value of _movementKey to
    //that key;
    if (newState.IsKeyDown(Keys.W) && !_oldState.IsKeyDown(Keys.W))
    {
        _movementKey = Keys.W;
    }
    else if (newState.IsKeyDown(Keys.D) && !_oldState.IsKeyDown(Keys.D))
    {
        _movementKey = Keys.D;
    }
    else if (newState.IsKeyDown(Keys.A) && !_oldState.IsKeyDown(Keys.A))
    {
        _movementKey = Keys.A;
    }
    else if (newState.IsKeyDown(Keys.S) && !_oldState.IsKeyDown(Keys.S))
    {
        _movementKey = Keys.S;
    }
}

_oldState = newState;

// ######################
// using the input to dictate direction
// ######################

//The above code simply keeps track of which key should be dictating the
//movement. Each frame check the '_movementKey' and use it to dictate the 
//movement. You could do this in the above code but I like to separate 
//handling the the input into 2 parts: interpreting it and applying it.

//This applies it while the above function interprets it. 
switch (_movementKey) {
    case Keys.W:
        //move up
        break;
    case Keys.D:
        //move right
        break;
    case Keys.S:
        //move down
        break;
    case Keys.A:
        //move left
        break;
    case Keys.None:
        break;
}
\$\endgroup\$
  • \$\begingroup\$ First, thanks alot for replying. Could you please expand your explenation on the code? I'm having a bit of trouble understanding it. \$\endgroup\$ – Shai Roemi Jul 19 '14 at 21:10
  • \$\begingroup\$ Is that more clear? Also I'll be in the chat room for an hour or two if you want to pop and discuss it. \$\endgroup\$ – ClassicThunder Jul 19 '14 at 21:43
  • \$\begingroup\$ It is defenitly more clear, and more important it helped me alot! Thank you so much for your help, very much appriciated. \$\endgroup\$ – Shai Roemi Jul 19 '14 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.