像游戏直升机或像 Left 4 dead 生存模式一样,我希望分数保持不变,只有在超过分数时才会改变。这是我到目前为止的代码。./m 文件
_score = 0;
_oldScore = -1;
self.scoreLabel = [CCLabelTTF labelWithString:@"" dimensions:CGSizeMake(100, 50) alignment:UITextAlignmentRight fontName:@"Marker Felt" fontSize:32];
_scoreLabel.position = ccp(winSize.width - _scoreLabel.contentSize.width, _scoreLabel.contentSize.height);
_scoreLabel.color = ccc3(255,0,0);
[self addChild:_scoreLabel z:1];
if (_score != _oldScore) {
_oldScore = _score;
[_scoreLabel setString:[NSString stringWithFormat:@"score%d", _score]];
}
和 .h 文件
int _score;
int _oldScore;
CCLabelTTF *_scoreLabel;
我试着把
_score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];
[[NSUserDefaults standardUserDefaults] setInteger:_oldScore forKey:@"score"];
[[NSUserDefaults standardUserDefaults] synchronize];
但是当我这样做时,它只保存了数据并继续上升而不是重新开始,只有在超过分数时才会改变。