【问题标题】:Save the high score cocos2d保存高分cocos2d
【发布时间】:2013-03-08 16:20:56
【问题描述】:

我试图更好地解释这种情况。

变量是:

int punteggio;

CCLabelTTF *labelPunteggio;

然后在初始化方法中,我在屏幕上打印我的分数:

- (id)init {
    if ((self = [super init])) {

    // PUNTEGGIO
    labelPunteggio = [CCLabelTTF labelWithString:@"0000" fontName:@"Marker Felt" fontSize:13];

    [self addChild:labelPunteggio];
    ....
    }
}

这就是在Punteggio上添加分数的功能:例如,我每杀死一个怪物我就加10分。

-(void)aggiungiPunti
{
    punteggio = punteggio +0001;

    [labelPunteggio setString:[NSString stringWithFormat:@"%d", punteggio]];
}

但是现在,我不知道当玩家完成游戏时如何保存分数。 我想保存这个分数,然后在屏幕上打印高分, 我在想

-(void) setScore:(int)score
{
    punteggio = highScore;

    if (punteggio>highScore)
    {
        highScore = punteggio;
    }
}

谢谢!

【问题讨论】:

    标签: iphone xcode cocos2d-iphone


    【解决方案1】:

    使用 NSUserdefaults

    // Snippet used to save your highscore in the prefs.
    int highScore  = yourGameScore;
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    //在游戏结束画面中

    // Get your highscore from the prefs.
    highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];
    

    【讨论】:

    • 错误是:[[NSUserDefaults standardUserDefaults] 同步]行上的预期“]”;
    • 对我来说没有显示任何错误。确保您添加了#import 及其文件扩展名 .m 或 .mm
    【解决方案2】:

    看看这个link,您可以使用SettingManager 类为您完成这项工作。我已经使用 settingManager 类来存储高分。 希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      相关资源
      最近更新 更多