您好,这是我第一次在这里发帖,对编码比较新。我已经玩了一两个月的java和libgdx,我对屏幕上的一件事感到困惑。
如果我有一个GameScreen类在玩游戏时处于活动状态,然后想要暂时切换到另一个屏幕以进行库存或暂停屏幕等,如果我让游戏切换回游戏屏幕,那么似乎一切都会重置一场新游戏开始了。什么是确保屏幕重新加载的正确方法是什么?
答案 0 :(得分:0)
如果您希望在后台保持屏幕处于活动状态(如您所述),则需要确保将游戏初始化代码分开,以便仅在启动新级别时调用它。有一百万种方法可以做到这一点。这是一个例子:
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.
}
}
然后在您的游戏课程中,您还需要确保保留游戏画面实例并重复使用它。
不要这样做:
setScreen(new MyGameScreen());
请改为:
//member variable:
private MyGameScreen myGameScreen;
//In some method:
if (myGameScreen == null)
myGameScreen = new MyGameScreen();
setScreen(myGameScreen);