更新并保存HighScore

时间:2016-04-08 00:54:54

标签: objective-c sprite-kit nsuserdefaults

整晚都是高分榜。我可以让它更新,但它不会在游戏之间保存,也不会在应用关闭时保存。即如果你得到8分,那么你的得分/高分是8分。如果你在接下来的比赛中获得3分你的得分是3而高分则是3当它应该仍然是8.我错过了什么?

    //Score Display

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    int _score = [prefs integerForKey:@"score"];
    SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"ChalkDuster"];
    //Other label node configuration here
    scoreLabel.position = CGPointMake(self.size.width/2,325);
    scoreLabel.fontColor = [SKColor blackColor];
    scoreLabel.text = [NSString stringWithFormat:@"Score: %d",_score];
    [self addChild:scoreLabel];

    //High Score
    NSUserDefaults *prefs2 = [NSUserDefaults standardUserDefaults];
    int _highscore = [prefs2 integerForKey:@"highscore"];
    SKLabelNode *highScoreLabel = [SKLabelNode labelNodeWithFontNamed:@"ChalkDuster"];
    highScoreLabel.position = CGPointMake(self.size.width/2,275);
    highScoreLabel.fontColor = [SKColor blackColor];
    highScoreLabel.text = [NSString stringWithFormat:@"High Score: %d",_highscore];
    [self addChild:highScoreLabel];

    if(_score > _highscore){
        _highscore = _score;
                 int _highscore = [prefs2 integerForKey:@"highscore"];
                highScoreLabel.text = [NSString stringWithFormat:@"High Score: %d",_highscore];
        NSUserDefaults *prefs2 = [NSUserDefaults standardUserDefaults];



    }

1 个答案:

答案 0 :(得分:0)

你正在检查_score> _高分 但你永远不会把高分回到prefs2。你正在读它,但没有把它放进去。

需要类似的东西     [prefs2 setInteger:_highscore forKey:@"hightscore"]