Cocos2d iphone Can';t在Cocos2d中使用NSUserDefaults保存HighScore
我正在用Cocos2d和SpriteBuilder制作游戏。我有问题:(。 例:第一次我的分数是8,高分是8。然后,我重试游戏,我的分数>8,当我死==>错误显示:( 我的高分来源:Cocos2d iphone Can';t在Cocos2d中使用NSUserDefaults保存HighScore,cocos2d-iphone,nsuserdefaults,Cocos2d Iphone,Nsuserdefaults,我正在用Cocos2d和SpriteBuilder制作游戏。我有问题:(。 例:第一次我的分数是8,高分是8。然后,我重试游戏,我的分数>8,当我死==>错误显示:( 我的高分来源: NSNumber *highScore = [[NSUserDefaults standardUserDefaults] objectForKey:@"_highscore"]; if (self.score > [highScore intValue]) { // new h
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];
}
我使用这个代码来获得/设置高分。它在已发布的游戏中起作用。希望它有帮助
+(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;
}
你说的BUG是什么意思?它崩溃了吗?它没有更新highscore吗?我认为它崩溃了:(第一次在Xcode中运行程序时,更新新的highscore是可以的。然后我重试游戏,让score>highscore-->BUG****[[NSUserDefaults standardUserDefaults]setObject:highscore forKey:@“\u highscore”];你认为它崩溃了吗?是还是不是?如果它确实发布了崩溃消息和日志。