Search code examples
user-interfacelibgdxscene2dhud

LibGDX-Scene2D: How to make hud with multiple buttons?


I'm making an Idle Game but now I don't know how to make the listeners of the HUD. It's okay to instantiate an InputListener() in every button of my hud? I have like 20 buttons or actors to touch.


Solution

  • Simply use addListener() method for every actor that you want to be clicable. I used to use ClickListener for this purpose although it is sometimes recommended to use ChangeListener due to better behaviour when the button is being disabled.

    So what you need to do is just

        Button button;
    
        //creating button...
    
        button.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y)
            {
                //Do something
            }
        });
    

    and the same for another buttons/actors on your HUD stage.

    You can also take a look at this thread where I have asked about performance of many listeners.