简体   繁体   中英

How can I handle multiple touchDragged events in libgdx?

I've just finished a simple Android tutorial here .
When you press in the top right corner, the character jumps. This is the code I'm using.

InputProcessor JUMP_PRESS = new InputProcessor() {

    ...

    @Override
    public boolean touchDown(int x, int y, int pointer, int button) {
        if((x > (5*width)/7 && y < (2*height)/7)) {
            controller.jumpPressed(); //make the character jump
        }
        return false;
    }

    @Override
    public boolean touchUp(int x, int y, int pointer, int button) {
        if (!Gdx.app.getType().equals(ApplicationType.Android))
            return false;
        if((x > (5*width)/7 && y < (2*height)/7)) {
            controller.jumpReleased();
        }
        return false;
    }

   ...

};

    ...some other processors for moving left and right...

InputMultiplexer mx = new InputMultiplexer();
    ...add the processors...

    ...

Gdx.input.setInputProcessor(mx);

I've noticed that when the finger is moved out of the jump area while still pressing the screen, the character will continue to jump until touchReleased() is finally called in that area.

So what I did to try to fix that is add a touchDragged() method in each processor:

public boolean touchDragged(int x, int y, int pointer) {
    if (!Gdx.app.getType().equals(ApplicationType.Android))
        return false;
    if(controller.getKeyFromHashMap(BobKeys.JUMP)) { //checks if currently jumping
        if(!(x > (5*width)/7 && y < (2*height)/7))
            controller.jumpReleased();
    }
    return false;
}

This solves the continuous jumping problem, but now unfortunately I cannot jump and move at the same time (before the touchDragged() method, I could) – if I move left and then jump, the character's entire horizontal motion stops immediately.

How can I handle multiple touchDragged events without each one stopping the next?

Thanks :)

What I would do is create a while loop that tests if the pointer is in that box and then if it goes out of your area stop jumping.

So I would put the following code into a while loop.

 @Override
    public boolean touchDown(int x, int y, int pointer, int button) {
        if((x > (5*width)/7 && y < (2*height)/7)) {
            controller.jumpPressed(); //make the character jump
        }
        return false;
 }

How about just to remember pointer when touching down and check that on touch up instead of coordinates? Something like this:

private int jumpPointer;

@Override
public boolean touchDown(int x, int y, int pointer, int button) {
    if((x > (5*width)/7 && y < (2*height)/7)) {
        controller.jumpPressed(); //make the character jump
        jumpPointer = pointer;
    }
    return false;
}

@Override
public boolean touchUp(int x, int y, int pointer, int button) {
    if (pointer == jumpPointer && Gdx.app.getType().equals(ApplicationType.Android))
        controller.jumpReleased();
    }
    return false;
}

Well, pointer is a bit oddly named in my opinion. TouchIndex would probably have made more sense, especially with pointer having a pair of very well defined meanings already. The pointer value is value between 0 and n (defined as 20 in LibGDX, in reality much lower) that represents the ORDER in which the touch event occurred in the event of multiple simultaneous touches. Therefore if you have multiple fingers touching, a pointer value of 0 would indicate that this touch event represents the first finger to touch the screen, while a value of 3 would be the fourth finger to touch the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM