Save game state after pause. AndEngine

2019-03-21 15:37发布

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 Pause\Resume the game (after power key pressed for example) in andEngine ?

1条回答
神经病院院长
2楼-- · 2019-03-21 15:46

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.

查看更多
登录 后发表回答