// global variables
int gLevel;
int gScore;
in my app delegate:
// if this is the first time ever running
// this app we need some default defaults
+ (void)initialize{
NSDictionary *appDefaults = [NSDictionary
dictionaryWithObjects:[NSArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:1],
nil]
forKeys:[NSArray arrayWithObjects:
@"Score", // starts at score of 0
@"Level", // starts at level 1
nil]];
[[NSUserDefaults standardUserDefaults]
registerDefaults:appDefaults];
}
// we are quitting
// save current score and level to the defaults
- (void) applicationWillTerminate:(UIApplication*)application
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber
numberWithInt:gScore] forKey:@"Score"];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber
numberWithInt:gLevel] forKey:@"Level"];
...
}
// we are starting up
// get the score and level
// that we saved when we last quit
// or if its the first time ever running this game
// it will load the default defaults
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
gScore=[[NSUserDefaults standardUserDefaults]
integerForKey:@"Score"];
gLevel=[[NSUserDefaults standardUserDefaults]
integerForKey:@"Level"];
...
}
leothenerd 3:14 pm on December 17, 2009