gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:15:02 24 4
gpt4 key购买 nike

我怎样才能保存我的乐谱字符串然后再调用它?我已经看到很多关于这个问题的答案,但它们都在 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;
}

最佳答案

按照 LearnCocos2D 的建议,将分数保存到 NSUserDefaults 中

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

您可以通过以下方式从 NSUserDefaults 中检索分数

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

关于ios - 保存和检索分数字符串 Cocos2d 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com