且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

暂停后保存游戏状态.和引擎

更新时间:2022-10-18 10:42:00

我找到的解决方案是将引擎的状态序列化为 JSON 并将其保存为共享存储.它的工作量很大,但它确实节省了它.由于andengine会在暂停后重新初始化你的引擎和纹理,我认为没有太多选择,除非你想为你的游戏重新编写一些andengine.(你可以)

在我的 JSON 中,我记录了每个精灵的位置类型速度等.保存内容的复杂性取决于游戏的复杂性.然后我为每个类添加了一个反序列化方法,该方法接受 JSON 作为输入.

作为该方法的一个脚注,我不能高度评价令人惊叹的 GSON 库,因为它使任务变得更容易.http://code.google.com/p/google-gson/>

但请记住,在恢复游戏时,将恢复状态设置为暂停,以便用户知道游戏何时重新开始.

i've found that after resuming the game all the engine is reloaded and application is reinitialized. I want to save engine state and in "onResume" resume the game proccess.

I've tried to save all the engine (mEngine) and then after resume the game in "onLoadEngine" return saved engine. I think it is a bad solution and it doesn't work

What is the best solution for PauseResume the game (after power key pressed for example) in andEngine ?

The solution I found was to serialize the state of the engine as JSON and save it as shared storage. Its a lot of work, but it does save it. Since andengine is going to re-init your engine and textures after a pause, I think there are not many choices, unless you want to re-write some of andengine for your game. (which you can)

In my JSON I record the position type velocity etc of each sprite. The complexity of what gets saved depends on the complexity of your game. And then to each class I added a deserialize method that accepts the JSON as input.

As a footnote to that method, I cannot speak highly enough of the amazing GSON library for making that an easier task. http://code.google.com/p/google-gson/

But remeber when restoring the game, to set the restored state to paused so the user knows when the game will re-commence.