保存高分cocos2d

时间:2013-03-08 16:20:57

标签: iphone xcode cocos2d-iphone

我试着更好地解释一下情况。

变量是:

int punteggio;

CCLabelTTF *labelPunteggio;

然后在init metod中我在屏幕上打印我的分数:

- (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;
    }
}

谢谢!

2 个答案:

答案 0 :(得分:2)

使用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 ];

答案 1 :(得分:0)

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

相关问题