1
\$\begingroup\$

I have 3 different buttons, one for each action. I want to be able to jump and walk at the same time, but there's a little problem. jumpButton doesn't work while I'm pressing leftButton or rightButton but the opposite works.

I have no idea what I'm doing wrong.

public void handleInput() {
    float x0 = (Gdx.input.getX(0) / (float)Gdx.graphics.getWidth()) * 960;
    float x1 = (Gdx.input.getX(1) / (float)Gdx.graphics.getWidth()) * 960;
    float y0 = (Gdx.input.getY(0) / (float)Gdx.graphics.getHeight()) * 640;
    float y1 = (Gdx.input.getY(1) / (float)Gdx.graphics.getHeight()) * 640;

    boolean jumpButton = (Gdx.input.isTouched(0) && x0 > 850 && x0 < 850+80 && y0 > 640-80);
    boolean leftButton = ( ((Gdx.input.isTouched(0) && x0 < 25+80) || (Gdx.input.isTouched(1) && x1 < 25+80)) && ((Gdx.input.isTouched(0) && x0 > 25) || (Gdx.input.isTouched(1) && x1 > 25)) && ((Gdx.input.isTouched(0) && y0 > 640-80) || (Gdx.input.isTouched(1) && y1 > 640-80)));
    boolean rightButton = ( ((Gdx.input.isTouched(0) && x0 < 125+80) || (Gdx.input.isTouched(1) && x1 < 125+80)) && ((Gdx.input.isTouched(0) && x0 > 125) || (Gdx.input.isTouched(1) && x1 > 125)) && ((Gdx.input.isTouched(0) && y0 > 640-80) || (Gdx.input.isTouched(1) && y1 > 640-80)));

    if (jumpButton) {
        // player jumps
    }
    if (leftButton) {
        // player walks to the left
    } else if (rightButton) {
        // player walks to the right
    } else {
        // player stops walking
    }
}
\$\endgroup\$
  • \$\begingroup\$ Have you tried moving the leftButton if statement on top of the jumpButton if statement, does it work the same? \$\endgroup\$ – Vadim Tatarnikov Aug 1 '15 at 20:33
  • \$\begingroup\$ Yes, it doesn't work either \$\endgroup\$ – sok Aug 1 '15 at 20:35
  • \$\begingroup\$ If I comment the "player stops walking" I will be able to Jump and Walk because the player never stops walking, but I just want to walk when the player presses to walk. It looks like it is a problem with the jumpButton because it has the false value when I'm pressing leftButton/rightButton \$\endgroup\$ – sok Aug 1 '15 at 20:47
  • \$\begingroup\$ So your problem is still that your player can not jump while walking right? \$\endgroup\$ – Vadim Tatarnikov Aug 1 '15 at 20:54
  • \$\begingroup\$ My problem is that when I'm holding right/left button to walk I press the jumpButton and nothing happens, but I can do the opposite, press right/left button while I'm holding the Jump button \$\endgroup\$ – sok Aug 1 '15 at 20:56
2
\$\begingroup\$

Based on my memories and for example this:

While it's difficult to understand the complicated ANDs and ORs, it seems clear that you forgot to check for the jumpButton being pressed after the other buttons.

With leftButton and rightButton, you have Gdx.input.isTouched(0) as well as Gdx.input.isTouched(1) (with OR inbetween them, so I take that as you're checking whether it was the first thing touched or the second). You don't do that with jumpButton, so you only check for the first finger there.

PS: I'd suggest making the code little more clear, for example iterating throught the pointer index (although it's just two options, so...), it would help you aswell. :)

\$\endgroup\$
  • \$\begingroup\$ +1 for more clear code. Always try to deconstruct long lines and functions into more readable lines or multiple methods. Keep it clear for others but especially yourself. \$\endgroup\$ – Madmenyo Aug 3 '15 at 8:39

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.