Reputation: 2381
I am new in iOS development and developing a Maths game in which i have 3 levels Easy, Normal and Hard. I want to save high-score programatically for each level but don't know how?
[[NSUserDefaults standardUserDefaults]integerForKey:@"highScore"]<[SingletonClass sharedSingleton].Score){
[[NSUserDefaults standardUserDefaults] setInteger:[SingletonClass sharedSingleton].Score forKey:@"highScore"];
[[NSUserDefaults standardUserDefaults]synchronize];
I am using this code for saving highscore. but this code saves same highscore for each level.
Upvotes: 1
Views: 171
Reputation: 3733
there is two way to store game score to creating Database
and storing score card array to NSUserDefault
.
To store score card array in NSUserDefault
.
[[NSUserDefaults standardUserDefaults] setValue:arrayHighScore forKey:@"highScore"];
[[NSUserDefaults standardUserDefaults] setValue:arrayMiddiumScore forKey:@"MediaumScore"];
[[NSUserDefaults standardUserDefaults] setValue:arrayEasyScore forKey:@"EasyScore"];
[[NSUserDefaults standardUserDefaults]synchronize];
Upvotes: 0
Reputation: 161
Using NSUserDefaults to save score would make you game easily hackable.
You can use NSKeyedArchiver to save your data to a file
-(void)saveState {
NSLog(@"Saving state");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *statePath = [documentsDirectory stringByAppendingPathComponent:someFileName];
NSMutableData *appData;
NSKeyedArchiver *encoder;
appData = [NSMutableData data];
encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:appData];
// SaveData using [encoder encodeObject:(id)object forkey(NSString *)key]
[encoder encodeObject:someUserObject forKey:@"someUserKey"];
[encoder encodeInteger:someUserscore forKey:@"someUserScoreKey"];
// Finish encoding and write the contents of gameData to file
[encoder finishEncoding];
[appData writeToFile:statePath atomically:YES];
}
You can then load the data back with file like so
-(void)loadState
{
NSLog(@"Loading state");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *statePath = [documentsDirectory stringByAppendingPathComponent:someFileName];
NSData *appData;
NSKeyedUnarchiver *decoder;
appData = [NSData dataWithContentsOfFile:statePath];
if (appData)
{
NSLog (@"file Loaded");
decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:appData];
// set data here using [decoder decodeObject for key...
someUserObject = [decoder decodeIntegerForKey:@"someUserKey"];
someUserScore = [decoder decodeObjectForKey:@"someUserScoreKey"];
}
else
{
NSLog(@"No saved data available or incorrect file name");
}
}
Any Custom classes that you encode/decode you need to make sure they conform to NSCoding.
Upvotes: 0
Reputation: 5845
This is more a question about how you structure your code/data than how you save it. Given that the high scores are an attribute of the player I would and an NSDictionary property to your user class (guessing that's the singlton) that can store all the high scores.
For example:
NSArray *keys = [NSArray arrayWithObjects:@"easy", @"medium", @"hard", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
Now you can store this NSDictionary in NSUserDefaults and when you retrieve it you have all scores.
For example:
[[NSUserDefaults standardUserDefaults] setObject:dictionary forKey:@"highScores"];
[[NSUserDefaults standardUserDefaults] synchronize];
Upvotes: 1
Reputation: 3905
You can use three separate keys for store highScore
- MGHighScoreEasy
, MGHighScoreNormal
and MGHighScoreHard
,
if(gameMode == MGGameModeEasy) {
[[NSUserDefaults standardUserDefaults] setInteger:[SingletonClass sharedSingleton].Score forKey:@"MGHighScoreEasy"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else if (....) {
}
Upvotes: 1