Iphone 保存高分cocos2d

Iphone 保存高分cocos2d,iphone,xcode,cocos2d-iphone,Iphone,Xcode,Cocos2d Iphone,我试图更好地解释情况 变量包括: int punteggio; CCLabelTTF *labelPunteggio; 然后在初始方法中,我在屏幕上打印我的分数: - (id)init { if ((self = [super init])) { // PUNTEGGIO labelPunteggio = [CCLabelTTF labelWithString:@"0000" fontName:@"Marker Felt" fontSize:13]; [s

我试图更好地解释情况

变量包括:

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;
    }
}
谢谢大家!

使用默认值

// 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 ];
看看这个,你可以使用类来为你做这项工作。我使用settingManager类来存储highscore。
希望这会有所帮助

错误是:在[[NSUserDefaults standardUserDefaults]synchronize]行上应出现“]”;对我来说,它没有显示任何错误。确保添加了#import及其文件扩展名.m或.mm