Search code examples
javaandroiduser-interfacebuttonlibgdx

How can I change what button I'm pressing without lifting my finger? (in LibGDX)


I am developing an app and i have followed this tutorial to add movement

but whenever i slide my finger to a new button it is still recognizing the button press for the button i firt pressed resulting in having to lift my finger to change buttons.

I would like to find a way to allow sliding to a new button

the only way i though of doing this is by creating a rectangle whenever the player presses the screen and if it overlaps the button which is of the Image class in libgdx and if it overlaps move in that direction and if the player moves their finger move the rectangle with it.

is there a better way to do this and if not how would i detect if it overlaps the Image.


Solution

  • What Arctic45 said works so in my class that handles touch controls i have for each button

    leftImg.addListener(new InputListener() {
    
            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                player.getComponent(MovementComponent.class).setGoLeft(true);
                super.enter(event, x, y, pointer, fromActor);
            }
    
            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                player.getComponent(MovementComponent.class).setGoLeft(false);
                super.exit(event, x, y, pointer, toActor);
            }
        });
    

    and when i run it and press a button then slide it to another button it changes what button is pressed without needed a touch up event.