LibGDX-Scene2D:如何制作带有多个按钮的hud?

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

我正在制作一个放置游戏,但现在我不知道如何制作 HUD 的听众。可以在我的 HUD 的每个按钮中实例化一个 InputListener() 吗?我有 20 个按钮或演员要触摸。

只需对每个您想要 clicable 的演员使用 addListener() 方法。我曾经使用 ClickListener for this purpose although it is sometimes recommended to use ChangeListener,因为当按钮被禁用时表现更好。

所以你需要做的只是

    Button button;

    //creating button...

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

你的 HUD 舞台上的另一个 buttons/actors 也是如此。

你也可以take a look at this thread我问过很多听众的表现。

这是一个 scene2d 按钮,它真的很容易使用,我不明白问题是什么或者为什么你担心每个按钮或 UI 对象上都有一个监听器,对我来说似乎很合乎逻辑.

button = new Button(buttonStyle);
    button.setPosition(x, y);
    button.addListener( new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            Gdx.app.log(TAG, "Button clicked");
        };
    });