0

分数根本没有重置,我正在尝试制作一个高分计数器,每次你通过以前的高分时它都会更新。但是,即使在比赛结束后分数也会继续上升而不重置我想要高分直到被超越。我尝试在游戏中重设分数,但似乎没有什么好处。

游戏结束.m

   CGSize winSize = [[CCDirector sharedDirector] winSize];    
     _score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];           


 _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]];

[[NSUserDefaults standardUserDefaults] setInteger:_oldScore forKey:@"score"];

. m hello world 文件(游戏文件)

         CGSize winSize = [[CCDirector sharedDirector] winSize];    
     _score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];           


 _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]];

[[NSUserDefaults standardUserDefaults] setInteger:_oldScore forKey:@"score"];


_score =0;

然而,分数一直在上升。我希望高分一直保持到超过

4

1 回答 1

0
// get the previous highscore
  _previousScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];         
// get the current score from wherever you want
  int _currentScore???   
// check, if the current score is higher than the old
  if (_currentScore > _previousScore) {
// save the _currentScore in the userdefaults 
    [[NSUserDefaults standardUserDefaults] setInteger:_currentScore forKey:@"score"];
// now update you label with _currentScore
  }
于 2012-07-07T20:14:54.867 回答