更新并保存 HighScore

Updated and saving HighScore

整晚都在打高分。我可以让它更新,但它不会在游戏之间保存,也不会在应用程序关闭时保存。也就是说,如果你在那场比赛中得到 8 分,你的 score/high 得分是 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];



    }

您查看的是 _score > _highscore 但您永远不会将高分设置回 prefs2。您正在读回它,而不是将其放入。

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