Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIBGDX Android - button listener not working

I have been trying to add a button into my game, and even after all the solutions I have looked up, nothing seems to be working. The button is rendering fine, but it does nothing when I touch it.

I have been using an InputMultiplexer to set an input processor on the stage with the button added onto it as an actor.

Any help is appreciated.

this.stage = new Stage();

...

this.buttonReplay = new TextButton("Replay", buttonStyle);
    this.buttonReplay.setX(width / 2 - this.buttonReplay.getPrefWidth() / 2);
    this.buttonReplay.setY(height / 2 - this.buttonReplay.getPrefHeight() / 2);

this.stage.addActor(buttonReplay);

(Different class)

        multiplexer.addProcessor(menuDeath.getStage()); // Adds death menu to input processor

        Gdx.app.log("adddad", "added");

        menuDeath.getButtonReplay().addListener(new InputListener()
        {
            @Override
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button)
            {
                Gdx.app.log("dsad", "daDSAda");
                return true;
            }
            @Override
            public void touchUp (InputEvent event, float x, float y, int pointer, int button)
            {
                Gdx.app.log("dsad", "daDSAda");
            }
        });

When run, "added" is logged in the console which should mean that the stage has an input processor, however, the button doesn't seem to be working at all, and I have even tried using different listeners like ClickListener and ChangeListener

like image 219
user3461933 Avatar asked May 14 '26 19:05

user3461933


1 Answers

The multiplexer could have sent the input event to another listener first which consumes the input event by returning true.

You could first try to get rid of the multiplexer just to make sure you code is working fine by replacing

multiplexer.addProcessor(menuDeath.getStage());

with

Gdx.input.setInputProcessor(menuDeath.getStage());

That way your menuDeath stage is the only listener and should therefore handle the touchDown event correctly.

like image 88
laubed Avatar answered May 17 '26 08:05

laubed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!