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];
我嘗試過了,它仍然顯示的是同樣的比分:(任何其他建議? – Crazycriss
你可以發佈你在哪裏保存最高分的代碼? –
我張貼現在 – Crazycriss