Search code examples
iphonexcode4cocos2d-iphone

cocos2d High score it messed up


I try having my high score set up where if the high score is surpassed it will show up in high score and I have it some what figured out but right now as soon as high score is surpassed it will take the same amount of high score points to go up one point. For example you get one point for each kill, if the high score is 7 points it will take 7 more kills to go up to 8 points on the high score board. The score resets itself again as soon as you get the high score and takes the same amount of points in the highscore to go up once. Hope it makes sense.

here is the code

.h file

 int _score;
    int _oldScore;
   CCLabelTTF *_scoreLabel;
    @property (nonatomic, assign) CCLabelTTF *scoreLabel;

.m file

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

   [[NSUserDefaults standardUserDefaults] synchronize];          


      _score = 0;         

}

  }

Now I know the _score = 0; rested the board but it keep resetting as soon as you get the high score. Another example is if you have 12 points on high score board it will take 24 points worth of killing to go up to 13points on the high score.

One more thing if i take out _score=0; the score will keep stacking but won't start over.


Solution

  • I don't understand these lines:

     _score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];           
     _oldScore = -1;
    

    should not [[NSUserDefaults standardUserDefaults] integerForKey:@"score"] be the old score? and why do you set _oldScore to be -1?

    maybe I am missing something...