【问题标题】:How to set high score and only changes if it is surpassed如何设置高分,只有超过它才会改变
【发布时间】:2012-06-27 09:01:09
【问题描述】:

像游戏直升机或者像 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];        

但是当我这样做时,它只保存了数据并继续上升,而不是重新开始,只有在超过分数时才会改变。

【问题讨论】:

    标签: iphone xcode4 cocos2d-iphone


    【解决方案1】:

    您需要比较您的分数是否超过旧分数,然后保存。

    例如,

    if (_score > _oldscore) {
      // Save out new score as it is more than the old score
    
      // Then reset ready for next time
      _oldscore = _score;
      _score = 0;
    }
    

    但是,如果您正在为这样的事情苦苦挣扎,我建议您将面临很多问题,除非您在进一步开发之前停下来学习编程基础知识。

    【讨论】:

    • 如果你能展示你当时尝试过的东西会很好 - 这样我们就不会浪费时间试图通过告诉你已经尝试过的东西来帮助你。您显然没有重置 _score,也没有将 _oldscore 设置为以前的 _score
    猜你喜欢
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多