I am a developer who is just learning to develop a game for the iPhone using ObjC. My understanding it runs only one app at a time. I would like to know what others do when a game is interrupted (ie the gamer switches to another app or to answer phone). Does the iPhone allow anyway of saving the state of your app so you can come back and continue? Any ideas or a link are appreciated.
Tutotrial is good to show how to save prefs out. However my question is for any suggestions when game is in play (E.g. billiard ball is moving, user has score running) and the user must switch out. I would like then for the user to come back in and continue where he left off. Is there an event that tells you the game apps is about to be exited to save the state of your game? Or is the only way to do it is by constantly writing out data to a state file.
Code: - (void)applicationWillTerminate:(UIApplication *)application{ // Your save code here } Put that method into your AppDelegate.m file and your game aught to be saved when the application quits. It doesn't hurt to do some periodic saving while the game is running though since the circumstances upon which this method is called are not always the same. (phone call, app crash, home btn, etc...)
You can also add an observer for the termination notification: Code: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState) name:UIApplicationWillTerminateNotification object:nil];
You can save game state with NSCoder When it comes time to actually save your game data, you can use the NSCoder protocol. It's easy to define which fields to save and which fields to fill from the default constructor. This is the entirety of my code to save a level: Code: - (void) encodeWithCoder: (NSCoder *)coder { [coder encodeObject: currentLevelName]; //code the level name [coder encodeObject: [NSNumber numberWithInt: [eventList count]] ]; // code the length of the event list [coder encodeObject: charList]; // code the list of characters } I don't bother saving a list of upcoming events in the level, or the info on the level itself - that can all be reloaded if you know the level name. I just save the level name, how far through the level we are, and the list of characters on the screen. That list is an NSMutableArray of custom "Character" objects; they need to implement encodeWithCoder too. Again, save only what you need. I don't save the max health or the animation info for each char, because I can easily rebuild them if I know the type of the character. I only save things that may have changed, like position and current health. Here's the code to reload the map from the coder: Code: // reads the map back in from an NSCoder. - (id) initWithCoder: (NSCoder *) coder { [super init]; self.currentLevelName = [coder decodeObject]; // load the level name int eventsRemaining = [[coder decodeObject] intValue]; // load the current event number //skip events until we get to the current event number int length = [eventList count]; NSRange deletionRange = NSMakeRange(0, length-eventsRemaining); [eventList removeObjectsInRange:deletionRange]; NSArray *tempCharList = [coder decodeObject]; //get the list of characters //add chars to map, set team counts for (id newChar in tempCharList) [self addChar: newChar]; return self; } Now I can load or save an entire game with one line: Code: //save the game [NSKeyedArchiver archiveRootObject: map toFile:filePath]; //load the game map = [[NSKeyedUnarchiver unarchiveObjectWithFile: newPath] retain];
OR, you can save the score with by saving the UILabel and you could have a hidden UILabel that shows the point of the billiard ball. Save the hidden UILabel and put the ball back to the point of the hidden UILabel when it opens again. If you need some more help, you can PM me.
I use NSMutableDictionary and write it out to my file. Yes, I do have to do my own encoding usually using to an NSData certain objects. I guess it boils down to same thing. Then read it back and decode on my own. Good stuff though. Thanks