Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageButton doesn't seem to detect clicks (Scene2d.ui)

When trying to put a simple ImageButton on stage, It didn't seem to detect clicks.

ImageButton btnStart = new ImageButton(ButtonArt.UP, ButtonArt.DOWN));

// btnStart.setClickListener(new ClickListener() {
//          @Override
//          public void click(Actor a, float arg1, float arg2) {
//             a.visible = false;
//          }
//       });

stage.addActor(btnStart);

ButtonArt.UP and ButtonArt.DOWN are TextureRegions, of each state. Now when I click on the button, it doesn't change state! I also tried the above ClickListener (for testing), but it seemed that didn't work either.

In my render method I just call stage.act() and stage.render(). I also tried drawing the TextureRegions with SpriteBatch in my render method, and they are in fact different textures.

Am I doing something wrong?

like image 832
user717572 Avatar asked Feb 20 '23 15:02

user717572


1 Answers

You will need to set the stage as your inputprocessor:

Gdx.input.setInputProcessor(stage);

If you need to have multiple inputprocessors (e.g., you need clicks registered outside your scene), you will need to use an InputMultiplexer, like this:

InputMultiplexer plex = new InputMultiplexer();
plex.addProcessor(myOtherProcessor);
plex.addProcessor(stage);
Gdx.input.setInputProcessor(plex);
like image 85
Matsemann Avatar answered Feb 23 '23 05:02

Matsemann