Resume game after phone call - jonbeebe?

I can see there is a “applicationSuspend” and “applicationResume” event type but still not quite sure how others are resuming their games after receiving a phone call such as done in “DoodleJump”

If I receive a phone call on my game, after the call ends, my game appears to relaunch whereas with other games, after the phone call, the games appear to be on the same screen they were on before the phone call.

I would really appreciate some advice on how this is achieved.

Thanks

Paul [import]uid: 7863 topic_id: 5147 reply_id: 305147[/import]

Basically you save the game state every x seconds. When the game launches, you check if there’s any game state saved, and if there is, you resume from there.

The value of x is up to your judgement, you have to find the right balance between saving the game state often and slowing down the game because of the game saving activity. [import]uid: 7026 topic_id: 5147 reply_id: 17033[/import]

Thanks radamanthus

Rather than save the game state every x seconds, would it not be possible to just save the game state when the applicationSuspend event takes place or is that not possible?

My game is structured using the Director Class and I have the following sections:

intro
Screen1 (menu screen)
Screen2 (game)

What I was attempting to do was save some game state variables in a text file then when applicationResume event takes place, load in these variables and attempt to display the appropriate section. However does not appear to work so I am wondering if this is the right way to do it. I detect the applicationResume event in my main.lua file and then attempt to change scene based on which section has been saved to the external text file as the current section.

[import]uid: 7863 topic_id: 5147 reply_id: 17039[/import]

Hi Paul,

Saving just on applicationSuspend will work, but saving every x seconds will let you recover nicely from app crashes.

I forgot exactly where I got that “save app state every x seconds”. I picked it up back when I was in the Objective-C dark ages, either from Stanford CS 193P (http://www.stanford.edu/class/cs193p/cgi-bin/drupal) or from Cocos2d.

I haven’t really done this yet in Corona SDK, so all I can give at this point are theories :slight_smile:

[import]uid: 7026 topic_id: 5147 reply_id: 17042[/import]

Hi Radamanthus

Good point about crashes though so far (touch wood) the game hasn’t crashed on my phone.

I haven’t quite figured out how to simulate a suspend on my phone without calling it and answering it is expensive testing this:) [import]uid: 7863 topic_id: 5147 reply_id: 17052[/import]

Thank you jhocking

Good point about the home button - I guess people may have to quit a game for other reasons apart from just a phone call for example if the boss walks into the room:)

Hmm I think what I will try and implement is a new option asking if they would like to resume their previous game or start a new one. Hence if some saved game state data is found then give them this choice otherwise just default to the new game option.

This is definitely something most games require and would be a great asset to have in the code exchange. I am happy to add my implementation when it is finished but can’t promise it will be the best you can get. [import]uid: 7863 topic_id: 5147 reply_id: 17069[/import]

What I was attempting to do was save some game state variables in a text file then when applicationResume event takes place, load in these variables and attempt to display the appropriate section. However does not appear to work

This is mostly the way to do it (and what I’m planning to implement in my game soon) but I don’t think waiting for applicationResume is enough. I think you need to load the saved state when starting up a new game. So now it’ll always start from where the game left off no matter how the user stops the app (eg. hit the Home button) and only start from the beginning if the user had actually finished their previous game.

ADDITION: Note that there’s an event for applicationExit, not just applicationSuspend.
http://developer.anscamobile.com/reference/index/eventtype-0 [import]uid: 12108 topic_id: 5147 reply_id: 17065[/import]

I’m trying to implement this and something isn’t behaving right.

local function systemEvents(event)  
 print("systemEvent " .. event.type .. " -- " .. event.name)  
 if event.type == "applicationSuspend" then  
 print("pause")  
 pause(event)  
 elseif event.type == "applicationResume" then  
 print("resume")  
 pause(event)  
 elseif event.type == "applicationExit" then  
 print("exiting...")  
 elseif event.type == "applicationStart" then  
 print("starting...")  
 end  
 return true  
end  
  
Runtime:addEventListener("system", systemEvents)  

According to the docs, if I background the simulator I should get a Suspend event. If I foreground it, I should get a Resume event.

But with the simulator as the foreground app, I click on my editor to bring it forward, I don’t get any calls to the systemEvents function.

I’m hoping when they say Foreground/Background, that’s not the Unix hit CTRL-Z and type in the “bg” command.
[import]uid: 19626 topic_id: 5147 reply_id: 31891[/import]

I don’t know about those foreground/background events, but in the simulator you can suspend and resume using Command+down keys. [import]uid: 12108 topic_id: 5147 reply_id: 32010[/import]