2014-09-01 48 views
0

我正在使用Cocos2d和SpriteBuilder进行游戏。我有问题:(.EX:第一次我的分数是8,最高分是8.然后,我重试游戏,我的分数> 8,当我死==>错误显示:(无法使用Cocos2d中的NSUserDefaults保存HighScore

我与高分来源:

NSNumber *highScore = [[NSUserDefaults standardUserDefaults] objectForKey:@"_highscore"]; 
    if (self.score > [highScore intValue]) { 
     // new highscore 
     highScore = [NSNumber numberWithInt:self.score]; 

     [[NSUserDefaults standardUserDefaults] setObject:highScore forKey:@"_highscore"];--->BUG 

     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 
+0

你说的BUG是什么意思它会崩溃!?它不更新高分? – LearnCocos2D 2014-09-01 07:40:11

+0

我认为它崩溃:( 我第一次运行程序我n Xcode,更新新的highScore是好的。然后我重试游戏,得分> highScore ----> BUG **** [[NSUserDefaults standardUserDefaults] setObject:highScore forKey:@“_ highscore”]; – 2014-09-01 09:26:07

+0

你*认为它崩溃?做到还是做不到?如果它发布崩溃消息并记录。 – LearnCocos2D 2014-09-01 10:11:20

回答

0

我使用此代码来获取/设置高分它的工作在发布的游戏希望它可以帮助

+(void)setNewHighScore:(int)score{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setValue:[NSNumber numberWithInt:score] forKey:@"GameNameHighScore"]; 
} 

+(int)getHighScore{ 
    int highScore = 0; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    highScore = [[defaults valueForKey:@"GameNameHighScore"] intValue]; 
    return highScore; 
}