Hello,
I am coding my first iPhone game with cocos2D. And I want to save the best score of the player (just an NSInteger) in the app. What is the best and simplest way to keep this information ?
Thanks !
Hello,
I am coding my first iPhone game with cocos2D. And I want to save the best score of the player (just an NSInteger) in the app. What is the best and simplest way to keep this information ?
Thanks !
Just 1 integer?
// write
[[NSUserDefaults standardUserDefaults] setInteger:theInteger forKey:@"bestScore"];
...
// read
theInteger = [[NSUserDefaults standardUserDefaults] integerForKey:@"bestScore"];
(Usually games support top-10, and you'd want to box the NSInteger into an NSNumber, insert all 10 of them into an NSArray, and save the array.)
You could save name/value pairs to a plist via NSDictionary. Here's a tutorial:
http://www.dbuggr.com/leothenerd/cocos-saving-data-file-nsdictionary/
Yes, use NSInteger and store it in your preferences or a private file.
Store in user defaults with: [[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:highScore] forKey:@"HighScore"];
Retrieve With:
NSNumber * highScore = [defaults objectForKey:@"HighScore"];