Hey guys, so I’m using slick2d to build a little game.
This is my controls method:
public void useControlls(){
if (!(CoreGameState.inputHandler.isKeyDown(Input.KEY_W)) && !(CoreGameState.inputHandler.isKeyDown(Input.KEY_D)) && !(CoreGameState.inputHandler.isKeyDown(Input.KEY_A)) && !(CoreGameState.inputHandler.isKeyDown(Input.KEY_S))){
pc.moving = false;
}
if (CoreGameState.inputHandler.isKeyDown(Input.KEY_W)) {
pc.pDirection = PlayerDirections.North;
if(!pc.checkCollisionNorth()){
pc.y -= pc.maxSpeed;
CoreGameState.inputHandler.clearKeyPressedRecord();
pc.moving = true;
}
}
else if (CoreGameState.inputHandler.isKeyDown(Input.KEY_S)) {
pc.pDirection = PlayerDirections.South;
if(!pc.checkCollisionSouth()){
pc.y += pc.maxSpeed;
CoreGameState.inputHandler.clearKeyPressedRecord();
pc.moving = true;
}
}
else if (CoreGameState.inputHandler.isKeyDown(Input.KEY_A)) {
pc.pDirection = PlayerDirections.West;
if(!pc.checkCollisionWest()){
pc.x -= pc.maxSpeed;
CoreGameState.inputHandler.clearKeyPressedRecord();
pc.moving = true;
}
}
else if (CoreGameState.inputHandler.isKeyDown(Input.KEY_D)) {
pc.pDirection = PlayerDirections.East;
if(!pc.checkCollisionEast()){
pc.x += pc.maxSpeed;
CoreGameState.inputHandler.clearKeyPressedRecord();
pc.moving = true;
}
}
}
Pretty much speaks for itself.
So my problem is that the movement does not feel good due to the fact that if i pess W or S and then D or A he does not move left or right.
He continious moving up or down. So if pressed D or A first and then press W or S the character moves up or down.
What i want to achieve is that if I’m pressing W (i keep pressing the W) and then press D that the character moves right. So far thats only possible for D & A but not for W & S.
I can see the flaw in the code. It’s because W & S are in the if-else before A & D. I tried playing with booleans and restrictions but somehow nothing did work.
I need some advice how to fix that.