LibGdx关闭窗口

时间:2016-06-10 14:46:49

标签: java libgdx

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

1 个答案:

答案 0 :(得分:2)

你想要做的是在按钮上添加一个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);