应该在哪些方法中保存/加载游戏状态

发布于 2025-01-06 04:09:36 字数 856 浏览 5 评论 0原文

关于如何保存游戏状态的文章有很多,而且都非常好。但我有一个概念上的误解,我应该在哪里保存状态?

我的 libGDX 游戏有多个屏幕,其中一对是 MainMenuScreenMainSceneScreen,它们继承自 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()行。

  1. 我做错了什么?
  2. 我应该重写哪些方法?

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.

  1. What did I do wrong?
  2. What methods should I override?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2025-01-13 04:09:36

我认为您已经正确地缩小了范围,并且您应该在 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 your Game 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 the resume 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 the create method in your Game (which is only invoked at application startup) to get the first start of your game to have 'resume' behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文