2014-02-17 36 views
0

我已經設置NSUserDefaults來獲得分數並顯示它。但現在我試圖顯示最好的分數,但標籤保持顯示相同的整數顯示最高分Cocos2d 3.0

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

回答

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]; 
+0

我嘗試過了,它仍然顯示的是同樣的比分:(任何其他建議? – Crazycriss

+0

你可以發佈你在哪裏保存最高分的代碼? –

+0

我張貼現在 – Crazycriss