gpt4 book ai didi

java - 恢复 libGDX 中的屏幕,而不是创建新实例

转载 作者:行者123 更新时间:2023-12-01 04:44:07 25 4
gpt4 key购买 nike

libGDX 是否可以在屏幕 B 中按下后退按钮后恢复屏幕 A,而不创建屏幕 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。

最佳答案

没有“正确”的方法可以做到这一点。例如,如果您的屏幕不需要大量资源,那么您可以在 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);
}

关于java - 恢复 libGDX 中的屏幕,而不是创建新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com