保存和检索分数字符串Cocos2d 3.0

时间:2014-02-15 07:57:06

标签: ios cocos2d-iphone save

我怎样才能保存我的分数字符串然后再回忆起来?我已经看到了很多关于这个问题的答案,但他们都已经在Cocos2d的早期版本上了,我试图实现它们但没有运气:(

这是显示当前得分的代码

score=0;
scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"a2203.ttf" fontSize:21.0f];
scorelabel.positionType = CCPositionTypeNormalized;
scorelabel.color = [CCColor blackColor];
scorelabel.position = ccp(0.85f, 0.95f); // Top Right of screen
[self addChild:scorelabel];

感谢您的任何建议:D

新代码

- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);

int savedScore  = [[NSUserDefaults standardUserDefaults] integerForKey:@"score_key"];

CCLabelTTF *ahighScore = [CCLabelTTF labelWithString:[NSUserDefaults stringWithFormat:@"score: %d",score_key] fontName:@"a2203.ttf" fontSize:23.0f];
ahighScore.positionType = CCPositionTypeNormalized;
ahighScore.color = [CCColor blackColor];
ahighScore.position = ccp(0.5f, 0.90f);
[self addChild:ahighScore];

return self;
}

1 个答案:

答案 0 :(得分:1)

根据LearnCocos2D的建议,将分数保存到NSUserDefaults

[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"score_key"];
[[NSUserDefaults standardUserDefaults] synchronize];

您可以通过

从NSUserDefaults中检索分数
int savedScore  = [[NSUserDefaults standardUserDefaults] integerForKey:@"score_key"];