分数正在超过cocos2d

时间:2012-07-10 03:07:35

标签: iphone objective-c xcode ios5 cocos2d-iphone

高分不会停留在屏幕上,所以最重要的问题是我该怎么办,所以分数将保持在屏幕上直到超过并将更新得分如高分。

几天前我接受了另一位用户的建议,这看起来很简单,但是当我尝试这样做时却没有成功。我尝试了几乎所有不同类型的组合,没有豆类。所以看起来我需要某人帮助指出错误。

初​​始化

 _oldScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"]; 
 CGSize winSize = [[CCDirector sharedDirector] winSize];
    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) {
      [[NSUserDefaults standardUserDefaults] setInteger:_score forKey:@"score"];          
       [_scoreLabel setString:[NSString stringWithFormat:@"score%d", _score]];

到目前为止它的效果很好,除了旧的分数之外的所有东西都不会停留在屏幕上。我希望玩家知道高分是多少。 用户说使用不同的密钥创建一个新的nsuserdefaults来存储旧的分数。因此,具有最高分数的旧分数将保留在屏幕上。

1 个答案:

答案 0 :(得分:0)

//优先保存分数

int highScore  = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"score"];
[[NSUserDefaults standardUserDefaults] synchronize];

//从偏好中获取保存的值

highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"score"] intValue ];

//将分数值设置为ur cocos2d label

[_scoreLabel setString:[NSString stringWithFormat:@"%d", _oldScore]];