【问题标题】:Libgdx Screen ConfusionLibgdx 屏幕混乱
【发布时间】:2016-03-18 00:59:57
【问题描述】:

您好,这是我第一次在这里发帖,对编码还比较陌生。我一直在玩 java 和 libgdx 一两个月,但我对屏幕的一件事感到困惑。

如果我有一个在玩游戏时处于活动状态的 GameScreen 类,然后想暂时切换到另一个屏幕以进行库存或暂停屏幕等,如果我让游戏切换回游戏屏幕,似乎一切都重置了一场新的比赛开始了。确保屏幕重新加载到其刚刚处于的相同状态的正确方法是什么?

【问题讨论】:

  • 另外,如果我需要提供更多信息或者我不清楚,请告诉我。就像我说的,这是我第一次使用这个网站,而且我是编码新手,所以我不能 100% 确定我的问题是否正确。因此,我们会欢迎反馈,而不仅仅是投反对票:D
  • 没关系,我想通了,我很愚蠢:D

标签: java android libgdx


【解决方案1】:

如果您想像您描述的那样在后台保持屏幕处于活动状态,您需要确保将游戏初始化代码分开,以便仅在您开始新关卡时调用它。有一百万种方法可以做到这一点。这是一个例子:

public class MyGameScreen {

    public MyGameScreen (){
        //load textures and sounds here if you aren't managing them somewhere else.

        startNewGameRound();
    }

    @Override
    public void show (){
        //this is only for stuff you need to reset every time the screen comes back. 
        //Generally, you would undo stuff that you did in `hide()`.
    }

    public void startNewGameRound (){
        //Reset all your variables and reinitialize state for a new game round.
        //This is called in the constructor for first time setup, and then it 
        //can also be called by a button anywhere that starts a new round.
        //But do not load textures and sounds here. Do that in the constructor
        //so it only happens once.
    }

}

然后在您的 Game 类中,您还需要确保保留您的游戏屏幕实例并重复使用它。

不要这样做:

setScreen(new MyGameScreen());

改为这样做:

//member variable:
private MyGameScreen myGameScreen;

//In some method:
if (myGameScreen == null)
    myGameScreen = new MyGameScreen();
setScreen(myGameScreen);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2013-04-11
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多