Search code examples
javaandroidlibgdx

libgdx multiple pan events


In my game the user is supposed to move the player with one finger on the left side of the screen and is able to shoot with the right finger. The problem is that when the shooting, the player can't move anymore. I've found some related questions but none helped me. I tried using 2 different GestureListener classes. One of them checks if the right side of the screen is touched and the other checks the left side, still doesn't work.

This is the pan method of the first class

    public boolean pan(float x, float y, float deltaX, float deltaY) {
        lastTouch = new Vector2(x, y).sub(deltaX, deltaY);
        Vector2 newTouch = new Vector2(x, y);

        Vector2 delta = newTouch.cpy().sub(lastTouch);
        if(lastTouch.x < Gdx.graphics.getWidth()/2 && newTouch.x < Gdx.graphics.getWidth()/2) {
            stage.getPlayer().move(new Vector2(delta.x, -delta.y));
        }
        lastTouch = newTouch;
        return true;
    }

This is the method of the second class

    public boolean pan(float x, float y, float deltaX, float deltaY) {
        lastTouch = new Vector2(x, y).sub(deltaX, deltaY);
        Vector2 newTouch = new Vector2(x, y);

        Vector2 delta = newTouch.cpy().sub(lastTouch);
        if(lastTouch.x > Gdx.graphics.getWidth()/2 && newTouch.x > Gdx.graphics.getWidth()/2){
            if(shoot) {
                bullets.add(new Bullet(WorldUtils.createBullet(stage.getWorld(), stage.getPlayer().getBody().getPosition().x+70, stage.getPlayer().getBody().getPosition().y, new Vector2(delta.x, -delta.y)), new Vector2(delta.x, -delta.y)));
                shoot = false;
            }
            stage.getPlayer().turn(new Vector2(delta.x, -delta.y));
        }
        lastTouch = newTouch;
        return true;
    }

Thank you


Solution

  • The Listeners in LibGDX tend to use boolean return values to state weather that the input if finished being processed. As you return true the listener stops processing further actions.

    The GestureListener can signal whether it consumed the event or wants it to be passed on to the next InputProcessor by returning either true or false respectively from its methods.

    Try changing the return method to false and see if that fixes the issue Otherwise try convert it to use the touchDown and touchUp method that has a parameter pointer for the finger count