0

I'm working on a java game using LibGdx and I need your help.

Explanation: There is one issue that I have with the arrow keys. Let me explain how my code works first. So in my update method, I check to see if keys are pressed. And if they are I handle them. The thing is that my code checks on after the other. So the first arrow key it checks has priority over all the others since, if it is pressed, it will be called first. Ex:

public static void updateControls(){

    if(Gdx.input.isKeyPressed(Input.Keys.UP){
        //Make player move up...
    }
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN){
        //Make player move down...
    }
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT){
        //Make player move left...
    }
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT){
        //Make player move right...
    }

}

By the way, my game is tile base. When the player is done moving 1 tile, it checks the arrow key input again to see which direction it needs to go next.

What causes the problem: Whenever a player is moving left, right or down and is holding one of those buttons and decides to press up. Up will take priority and the player will start moving up even though the player was initially moving down.

I tried explaining this to my best abilities since it's pretty hard to comprehend without trying it yourself. if you need more explanation or a link to my game to try the glitch yourself, feel free to comment and ask for those things. Thanks!

Drew
  • 24,851
  • 10
  • 43
  • 78
vedi0boy
  • 1,030
  • 4
  • 11
  • 32
  • 2
    OK, so what do you actually want to happen? Do you want the player to continue in the same direction when another key is pressed? – Alex - GlassEditor.com Oct 13 '14 at 12:17
  • Yes exactly. But I fixed it already but thanks! Right after I posted this I figured out a solution. – vedi0boy Oct 13 '14 at 12:18
  • Solution: I added an int that recorded the last direction. Then whenever a key was pressed, it would check to see if the key of the last direction was still being pressed. If it was, it would run the code for that key again. if it wasn't, then it would just continue without interuptions. – vedi0boy Oct 13 '14 at 12:20
  • @vedi0boy if you have found a satisfactory solution, please feel free to answer your own question and mark it as Accepted Answer. – Compass Oct 13 '14 at 12:42

1 Answers1

0

All I did was I basically created an int that saves what was the last key to be pressed. Then whenever one of the arrow keys are pressed, it goes through a switch statement like this (This on is for when up is pressed):

switch(lastKey){
case DIRECTION_UP:
    break;
case DIRECTION_DOWN:
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN)
        handleDownPressed();
    break;
case DIRECTION_LEFT:
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)
        handleLeftPressed();
    break;
case DIRECTION_RIGHT:
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)
        handleRightPressed();
    break;
}

It checks to see what the last keys that was pressed was and if it is still pressed, it will handle it being pressed before handling up pressed. If the last key was up, it will break out of the switch and continue handling up input.

vedi0boy
  • 1,030
  • 4
  • 11
  • 32