tags:

views:

99

answers:

3

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 !

+3  A: 

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.)

KennyTM
Thanks, it's perfect!
micropsari
A: 

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/

Joost Schuur
A: 

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"];    
Kenny