比赛结束后,cocos2d得分没有重置

时间:2012-07-07 18:58:08

标签: iphone ios5 xcode4 cocos2d-iphone

分数根本没有重新设定,我正在尝试制作一个高分计数器,每当你通过之前的高分时它都会更新。但是,即使比赛过后,分数也会不断重新上升,我想要高分直到被超越。我尝试在游戏中重叠分数,但似乎没有好处。

gameover .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 file(游戏文件)

         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;
然而,分数一直在上升。我希望高分能够保持到超过

1 个答案:

答案 0 :(得分: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
  }