我对LibGdx有一个问题。我有一个“ MainGameScreen”类和一个普通的Window(scene2d Ui)类。我的窗口中有一个“ TextButton”。按下按钮时关闭窗口的最佳方法是什么?谢谢 !

最佳答案

您要做的是向按钮添加一个ChangeListener,每当按下按钮时,它就会关闭窗口。

这是一个如何演示的小演示:

// The window has to be final to be accessible from our listener.
final Window window = new Window("Title", skin);

// Create our button.
TextButton button = new TextButton("Press me to close window!", skin);

// Here we add a click listener to our button.
button.addListener (new ChangeListener() {
    // This method is called whenever the actor is clicked. We override its behavior here.
    @Override
    public void changed(ChangeEvent event, Actor actor) {
        // This is where we remove the window.
        window.remove();
    }
});

// Add the button to our window.
window.add(button);

// Add the window to our stage.
stage.addActor(window);

07-24 06:54
查看更多