【问题标题】:cocos2d score not resetting after game over游戏结束后cocos2d分数不重置
【发布时间】:2012-07-07 18:58:08
【问题描述】:

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

游戏结束.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;

但是分数一直在上升。我希望高分一直保持到超过

【问题讨论】:

    标签: iphone ios5 xcode4 cocos2d-iphone


    【解决方案1】:
    // 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
      }
    

    【讨论】:

    • 谢谢,只剩下一道题了,之前的高分就上不去了。有什么建议吗?
    • 是的,旧的高分将被覆盖。使用 NSUserDefaults 和另一个键来存储以前的高分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    相关资源
    最近更新 更多