我是iOS开发和开发数学游戏的新手,其中我有3个级别Easy,Normal和Hard。我想以编程方式为每个级别保存高分,但不知道如何?
[[NSUserDefaults standardUserDefaults]integerForKey:@"highScore"]<[SingletonClass sharedSingleton].Score){
[[NSUserDefaults standardUserDefaults] setInteger:[SingletonClass sharedSingleton].Score forKey:@"highScore"];
[[NSUserDefaults standardUserDefaults]synchronize];
我正在使用此代码来保存高分。但是这段代码为每个级别保存了相同的高分。
答案 0 :(得分:1)
您可以使用三个单独的密钥来存储highScore
- MGHighScoreEasy
,MGHighScoreNormal
和MGHighScoreHard
,
if(gameMode == MGGameModeEasy) {
[[NSUserDefaults standardUserDefaults] setInteger:[SingletonClass sharedSingleton].Score forKey:@"MGHighScoreEasy"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else if (....) {
}
答案 1 :(得分:1)
这是一个关于如何构建代码/数据的问题,而不是保存它的方式。鉴于高分是我想要的玩家的属性和你的用户类的NSDictionary属性(猜测那些可以存储所有高分的东西)。
例如:
NSArray *keys = [NSArray arrayWithObjects:@"easy", @"medium", @"hard", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
现在您可以将此NSDictionary存储在NSUserDefaults中,当您检索它时,您将获得所有分数。
例如:
[[NSUserDefaults standardUserDefaults] setObject:dictionary forKey:@"highScores"];
[[NSUserDefaults standardUserDefaults] synchronize];
答案 2 :(得分:0)
有两种方法可以将游戏分数存储到创建Database
并将记分卡数组存储到NSUserDefault
。
将记分卡数组存储在NSUserDefault
。
[[NSUserDefaults standardUserDefaults] setValue:arrayHighScore forKey:@"highScore"];
[[NSUserDefaults standardUserDefaults] setValue:arrayMiddiumScore forKey:@"MediaumScore"];
[[NSUserDefaults standardUserDefaults] setValue:arrayEasyScore forKey:@"EasyScore"];
[[NSUserDefaults standardUserDefaults]synchronize];
答案 3 :(得分:0)
使用NSUserDefaults来保存分数会让你的游戏很容易被破解。
您可以使用NSKeyedArchiver将数据保存到文件
-(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];
}
然后你可以用这样的文件加载数据
-(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");
}
}
您编码/解码的任何自定义类都需要确保它们符合NSCoding。