Search code examples
javauser-interfacelibgdxstage

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?


Solution

  • 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);