在libGDX中恢复屏幕,而不是创建新实例

时间:2013-04-19 13:12:18

标签: java libgdx

在没有创建新的屏幕A实例的情况下,在屏幕B中按下后退按钮后,libGDX中是否可以恢复屏幕A?这样我的玩家角色就可以从最后一个位置继续行走,而不是从起点走。 当用户从屏幕A导航到屏幕B时,屏幕A暂停,但游戏不是。

我通常在ScreenB类中使用以下代码在屏幕之间切换:

btnLabsEnter.addListener(new InputListener(){
                 @Override
                    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                      getGame().setScreen(new ScreenA(getGame()));
                          return true;
                 }
             });

然而,上面的代码用于创建屏幕A的新实例,而不是显示先前隐藏的屏幕A.

1 个答案:

答案 0 :(得分:0)

没有“正确”的方法来做到这一点。例如,如果您的屏幕不需要大量资源,那么您可以在 Game 类中预先创建它们,只需在它们之间切换而不创建新实例。

这就是我为Very Angry Robots所做的,两年前这款游戏在低端手机上运行得非常好。我不会每次都这样做,但对于资源轻量级游戏,它的效果非常好。

/** Creates all the screens that the game will need, then switches to the main menu. */
@Override
public void create () {
    Assets.load();
    mainMenuScreen = new MainMenuScreen(this);
    playingScreen = new WorldPresenter(this);
    scoresScreen = new ScoresScreen(this);
    setScreen(mainMenuScreen);
}