我不明白如何正确使用NSUserDefaults standardUserDefaults
。我有GameOver.m
个文件,如果得分高,则会检查得分,如果是,它会像这样保存:
if(score > highScore){
highScore =score;
bestScore = highScore;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:bestScore forKey:@"bestScore"];
[prefs synchronize];
}
要加载高分我使用:
NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
NSInteger theHighScore =[prefs integerForKey:@"bestScore"];
它可以工作,但是在关闭应用程序并重新打开高分后,我读到,加载过程需要在- (void)applicationDidEnterBackground:(UIApplication *)application
的AppDelegate文件中,我该怎么做?
修改
的 GameScene.m
-(void)Scoring {
NSUserDefaults *scoreprefs = [NSUserDefaults standardUserDefaults];
[scoreprefs setInteger:score forKey:@"score"];
}
GameScene.m
#import "GameScene.h"
@implementation GameOver
NSInteger score;
NSInteger highScore;
NSInteger bestScore;
- (id)initWithSize:(CGSize)size playerLose:(BOOL)isLose {
self = [super initWithSize:size];
if (self) {
NSUserDefaults *scoreprefs =[NSUserDefaults standardUserDefaults];
NSInteger score = [scoreprefs integerForKey:@"score"];
SKLabelNode *scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
scorelabel.text =[NSString stringWithFormat:@"%ld",(long)score];
// then i define position/fontsize for scoreLabel
[self addChild:scorelabel];
if(score > highScore){
highScore =score;
bestScore = highScore;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:bestScore forKey:@"bestScore"];
[prefs synchronize];
}
NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
NSInteger theHighScore =[prefs integerForKey:@"bestScore"];
SKLabelNode *highscorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
highscorelabel.text =[NSString stringWithFormat:@"%ld",(long)theHighScore];
//define position/fontsize for highscorelabel
}
return self;
}
就是这样,它。
答案 0 :(得分:1)
您没有获取高分数据。因此,以下条件每次都很好,因为highScore
变量将为0.在此阶段,即使bestScore
大于{{1},bestScore
也会被当前分数覆盖}}
score
答案 1 :(得分:1)
@interface GameOver ()
@property (nonatomic, assign) NSInteger score;
@property (nonatomic, assign) NSInteger highScore;
@property (nonatomic, assign) NSInteger bestScore;
@end
@implementation GameOver
@synthesize score,bestScore,highScore;
- (id)initWithSize:(CGSize)size playerLose:(BOOL)isLose {
NSUserDefaults *scoreprefs =[NSUserDefaults standardUserDefaults];
if (self = [super initWithSize:size]) {
if (self) {
score = [scoreprefs integerForKey:@"score"];
highScore = [scoreprefs integerForKey:@"bestScore"];
SKLabelNode *scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
scorelabel.text =[NSString stringWithFormat:@"%ld",(long)score];
// then i define position/fontsize for scoreLabel
[self addChild:scorelabel];
if(score > highScore){
highScore =score;
bestScore = highScore;
[scoreprefs setInteger:bestScore forKey:@"bestScore"];
[scoreprefs synchronize];
}
SKLabelNode *highscorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
highscorelabel.text =[NSString stringWithFormat:@"%ld",(long)highScore];
//define position/fontsize for highscorelabel
}
}
return self;
}
问题是你没有初始化你的HighScore,并且检查
score(assume 5242)>highScore(nil)
永远是真的
EDITED