【问题标题】:For the time being I need to save a score string for my game locally for cocos2d目前我需要在本地为 cocos2d 保存我的游戏的分数字符串
【发布时间】:2012-06-26 22:40:39
【问题描述】:

我将如何解决这个问题?我可以给你看我的一些代码。但基本上我猜我必须保存字符串或其他东西。任何提示将不胜感激。我希望它出现在另一个场景中。

.h 文件

          int _score;
          int _oldScore;
        CCLabelTTF *_scoreLabel;
         @property (nonatomic, assign) CCLabelTTF *scoreLabel;

.m 文件

       _score = 0;
       _oldScore = -1;
     self.scoreLabel = [CCLabelTTF labelWithString:@"" dimensions:CGSizeMake(100, 50)               alignment:UITextAlignmentRight fontName:@"Marker Felt" fontSize:32];
     _scoreLabel.position = ccp(winSize.width - _scoreLabel.contentSize.width,      _scoreLabel.contentSize.height);
     _scoreLabel.color = ccc3(255,0,0);
     [self addChild:_scoreLabel z:1];


if (_score != _oldScore) {
    _oldScore = _score;
    [_scoreLabel setString:[NSString stringWithFormat:@"score%d", _score]];



}

【问题讨论】:

    标签: iphone xcode4 cocos2d-iphone


    【解决方案1】:

    我认为最简单的方法是将其放入用户默认值中。例如

    // to write
    [[NSUserDefaults standardUserDefaults] setInteger:yourScore forKey:@"score"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // to read
    int score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];
    

    由于synchronize 步骤,这样做会产生一点成本,所以不要只这样做而不是使用普通的内部存储,也要这样做。

    The user defaults 用于存储用户在应用程序中选择的设置。它们的名称有点奇怪,因为在桌面上的假设是它们将用于存储与新文档关联的默认属性。您可以将各种类型的数据存储到用户默认值中,如果您愿意,包括NSStrings。

    standardUserDefaults 是操作系统固有的那些。它们是针对每个应用的,因此您无需担心命名冲突。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2016-06-23
    • 1970-01-01
    相关资源
    最近更新 更多