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\$
  • \$\begingroup\$ Have you seen this question: gamedev.stackexchange.com/questions/57573/…? \$\endgroup\$ – MichaelHouse Dec 21 '13 at 6:31
  • \$\begingroup\$ Yes I have, but I don't know how to use/implement it. Any help? \$\endgroup\$ – greenlittleapple Dec 21 '13 at 6:56
  • \$\begingroup\$ Have you tried InputMultiplexer() for multiple input processors? \$\endgroup\$ – daniel Dec 21 '13 at 18:52
  • \$\begingroup\$ This code should work, show the code when the velocity affects the position of your player. \$\endgroup\$ – Lestat Dec 22 '13 at 7:42
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\$
  • \$\begingroup\$ Since the game is looping, the character SLOWLY falls down! pastebin.com/dktzMsnB here's the if statement thingie \$\endgroup\$ – greenlittleapple Dec 23 '13 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\$ – Intellectual Dec 23 '13 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\$

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.