Search code examples
libgdx

How to display a text while hovering on image or texture in libgdx?


I am trying to implement a map of the universe. When mouse on a pic of the planet simply hovering on the picture, I want to see some information. How to handle this in libgdx? In the following example, I am trying to give mouseClick sound while hovering, still does not work.

   ImageButton imageButton = new ImageButton(drawable);
    imageButton.setSize(100,100);
    imageButton.setPosition(100,100);
    imageButton.addListener(new FocusListener() {
        @Override
        public boolean handle(Event event) {
            mouseClick.play();
            return true;
        }
    });

Here how my override methods @enter and @exit looks`, I created HoverListener and override both methods.

@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
    super.enter(event, x, y, pointer, fromActor);
    fromActor.setName("Hello");

}

@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
    super.exit(event, x, y, pointer, toActor);

    toActor.setName("Bye");

}

Then btn_station.addListener(new HoverListener()); Still no interaction :/


Solution

  • Check the following:

    • You stage is the input processor: Gdx.input.setInputProcessor(stage)
    • Your Actor is on the stage: stage.addActor(actor)
    • The size of your actor is set: actor.setSize(100f, 100f)
    • You handle input events correctly (implement and override relevant interfaces)

    As per your response to my comments, it sounds like in this case you did not set the stage as the input processor.