应该在哪些方法中保存/加载游戏状态
关于如何保存游戏状态的文章有很多,而且都非常好。但我有一个概念上的误解,我应该在哪里保存状态?
我的 libGDX 游戏有多个屏幕,其中一对是 MainMenuScreen 和 MainSceneScreen,它们继承自 Screen 类。 MainMenuScreen 在游戏开始时显示,MainSceneScreen 稍后显示。
问题是什么?我导航到 MainSceneScreen,强制 Android 停止应用程序(我更改了设备上的语言设置来实现此目的)。之后,我再次选择该应用程序,可以看到显示了MainMenuScreen。但我希望显示MainSceneScreen。
我想我应该重写 resume
方法。但哪一个呢?我有类 PsGame ,它扩展了 libgdx 的 Game 类。我在其 resume
方法中设置了断点,结果发现该方法没有被调用。我调查了这个问题,并在 libGDX 中 AndroidApplication 类的 onResume
方法中发现了这个奇怪的代码:
if (!firstResume)
graphics.resume();
else
firstResume = false;
我的调试器说 firstResume 是 true 并且没有转到graphics.resume()行。
- 我做错了什么?
- 我应该重写哪些方法?
There are a lot of articles about how to save a state of a game and they are pretty good. But I have one conceptual misunderstanding where should I save the state?
My libGDX game has number of screens and pair of them are MainMenuScreen and MainSceneScreen these are inherited from Screen class. MainMenuScreen is shown at start of the game the MainSceneScreen little later.
What is the problem? I navigated to MainSceneScreen, forced Android to stop the application (I change a language settings on the device to achieve this). After that I select the application again and I can see MainMenuScreen is shown. But I want MainSceneScreen to be shown.
I suppose I should override resume
method. But which one? I have class PsGame that extends Game class of libgdx. I put breakpoints to its resume
method and it turned out that method was not called. I investigated the problem and I've found this strange code in the onResume
method of AndroidApplication class in libGDX:
if (!firstResume)
graphics.resume();
else
firstResume = false;
My debugger said firstResume was true and didn't go to graphics.resume() line.
- What did I do wrong?
- What methods should I override?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您已经正确地缩小了范围,并且您应该在
Game
子类上的resume
方法调用期间恢复保存的状态。如果您切换到其他应用程序一段时间,然后切换回您的应用程序(不执行导致应用程序终止的步骤),则应该调用此函数。无论出于何种原因,libgdx 都不会转发初始启动的resume
调用(从长远来看,这种设计可能是个好主意,但在开始时会有点混乱)。从Game
中的create
方法调用恢复代码(仅在应用程序启动时调用)非常容易,以便在游戏首次启动时“恢复” ' 行为。I think you've narrowed this down correctly, and you should be restoring your saved state during the
resume
method call on yourGame
subclass. This should be called if you switch to some other app for a bit, and then switch back to your app (without going through the steps that will cause the app to be terminated). For whatever reason, libgdx does not forward theresume
call for the initial startup (this design is probably a good idea in the long run, but is a bit confusing when starting out). Its easy enough to invoke your restore code from thecreate
method in yourGame
(which is only invoked at application startup) to get the first start of your game to have 'resume' behavior.