# How can I make my sprite jump up and move forward at the same time?

I'm trying to make a sprite's movements work. For some reason, he can only jump and then move, or move and then jump, but never at the same time. I'm using InputProcessor and a TiledMap system. Here's some code :

@Override
public boolean keyDown(int keycode) {
switch(keycode) {
case Keys.W:
if(canJump) {
velocity.y = speed;
canJump = false;
}
break;
case Keys.A:
velocity.x = -speed;
break;
case Keys.D:
velocity.x = speed;
break;
}
return true;
}

@Override
public boolean keyUp(int keycode) {
switch(keycode) {
case Keys.W:
velocity.y = 0;
break;
case Keys.A:
velocity.x = 0;
break;
case Keys.D:
velocity.x = 0;
break;
}
return true;
}

• Have you seen this question: gamedev.stackexchange.com/questions/57573/…?
– House
Dec 21 '13 at 6:31
• Yes I have, but I don't know how to use/implement it. Any help? Dec 21 '13 at 6:56
• Have you tried InputMultiplexer() for multiple input processors? Dec 21 '13 at 18:52
• This code should work, show the code when the velocity affects the position of your player. Dec 22 '13 at 7:42

## 2 Answers

Create booleans W, A and D. Set them to true on keyDown and set them to false on keyUp, then in your game loop or update function, check the variables to see if the keys are being held and manipulate your sprite from there.

• Since the game is looping, the character SLOWLY falls down! pastebin.com/dktzMsnB here's the if statement thingie Dec 23 '13 at 2:53
• keyup is executed only one time on key release, it is not executed all time a key is not held. In your update function, your vertical velocity is set to zero every time it updates if the W key is not pressed. make the holding key events in the update loop and let the events that happen once the key is released in the keyUp function. Since you are resetting the vertical velocity every frame W isn't held down, only a portion of your gravity acts on the sprite :) Dec 23 '13 at 13:27

Ok guys, I think that the problem was that my computer can't detect the keypressess, but my friends tested it out and they could go diagonally! Thanks for all your suggestions, and sorry about wasting Internet space on a useless question, and have a Merry Christmas and Happy New Year!