我已经设置了NSUserDefaults以获得分数并显示它。但现在我正在尝试显示最佳分数,但标签仍然显示相同的整数
savedScoreMax = [[NSUserDefaults standardUserDefaults] integerForKey:@"max_score_key"];
highScoreLabelMax = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"High Score: %d",savedScoreMax] fontName:@"a2203.ttf" fontSize:21.0f];
highScoreLabelMax.positionType = CCPositionTypeNormalized;
highScoreLabelMax.color = [CCColor blackColor];
highScoreLabelMax.position = ccp(0.5f, 0.6f); // Top Right of screen
[self addChild:highScoreLabelMax];
savedScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"score_key"];
highScoreLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Score: %d",savedScore] fontName:@"a2203.ttf" fontSize:21.0f];
highScoreLabel.positionType = CCPositionTypeNormalized;
highScoreLabel.color = [CCColor blackColor];
highScoreLabel.position = ccp(0.5f, 0.4f); // Top Right of screen
[self addChild:highScoreLabel];
if(savedScoreMax>savedScore) savedScore = savedScoreMax;
这里有什么不对?代码运行良好,运行正常,但它覆盖相同的整数。谢谢:D
更新
- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair monsterCollision:(CCNode *)monster projectileCollision:(CCNode *)projectile {
[monster removeFromParent];
[projectile removeFromParent];
score++;
[scorelabel setString:[NSString stringWithFormat:@"score: %d",score]];
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"score_key"];
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"max_score_key"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
整合abhineetprasad代码的新代码
- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair monsterCollision:(CCNode *)monster projectileCollision:(CCNode *)projectile {
[monster removeFromParent];
[projectile removeFromParent];
score++;
[scorelabel setString:[NSString stringWithFormat:@"score: %d",score]];
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"score_key"];
maxScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"max_score_key"];
if(score > maxScore){
[[NSUserDefaults standardUserDefaults] setInteger:maxScore forKey:@"max_score_key"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
带有字符串的标签
savedScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"score_key"];
afterscorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Score: %d",score] fontName:@"a2203.ttf" fontSize:23.0f];
afterscorelabel.positionType = CCPositionTypeNormalized;
afterscorelabel.color = [CCColor blackColor];
afterscorelabel.position = ccp(0.5f, 0.60f);
[self addChild:afterscorelabel];
bestScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"max_score_key"];
bestscorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"High Score: %d",maxScore] fontName:@"a2203.ttf" fontSize:23.0f];
bestscorelabel.positionType = CCPositionTypeNormalized;
bestscorelabel.color = [CCColor blackColor];
bestscorelabel.position = ccp(0.5f, 0.50f);
[self addChild:bestscorelabel];
答案 0 :(得分:1)
您对savedScore和savedScoreMax使用相同的密钥。对savedScoreMax使用其他一些键,例如“max_score_key”。
NSUserDefaults基本上是一个可以有几个键值对的字典。您选择的密钥必须是唯一的,否则您最终将覆盖该密钥的值。
修改强>:
您正在将score
的值保存到这两个键,因此它们返回相同的值。我相信只有当您之前的最高分数记录被打破时,您才会想要保存到最高分数。
使用以下代码
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"score_key"];
int maxScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"max_score_key"];
if(score > maxScore){
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"max_score_key"];
}
[[NSUserDefaults standardUserDefaults] synchronize];