0
\$\begingroup\$

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;
}
\$\endgroup\$
4
  • \$\begingroup\$ Have you seen this question: gamedev.stackexchange.com/questions/57573/…? \$\endgroup\$
    – House
    Dec 21, 2013 at 6:31
  • \$\begingroup\$ Yes I have, but I don't know how to use/implement it. Any help? \$\endgroup\$ Dec 21, 2013 at 6:56
  • \$\begingroup\$ Have you tried InputMultiplexer() for multiple input processors? \$\endgroup\$
    – daniel
    Dec 21, 2013 at 18:52
  • \$\begingroup\$ This code should work, show the code when the velocity affects the position of your player. \$\endgroup\$
    – Daahrien
    Dec 22, 2013 at 7:42

2 Answers 2

0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Since the game is looping, the character SLOWLY falls down! pastebin.com/dktzMsnB here's the if statement thingie \$\endgroup\$ Dec 23, 2013 at 2:53
  • \$\begingroup\$ 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 :) \$\endgroup\$ Dec 23, 2013 at 13:27
0
\$\begingroup\$

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!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .