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);
1 回答
如果您希望在后台保持屏幕处于活动状态(如您所述),则需要确保将游戏初始化代码分开,以便仅在启动新级别时调用它 . 有一百万种方法可以做到这一点 . 这是一个例子:
然后在您的Game类中,您还需要确保保留游戏屏幕实例并重复使用它 .
不要这样做:
改为: