2013-03-08 71 views
0

我试图解释更好的情况。保存高分cocos2d

的变量是:

int punteggio; 

CCLabelTTF *labelPunteggio; 

然后在init梅托德打印我的成绩在屏幕上:

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

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 ]; 
+0

xcode给我的错误...我不知道为什么.. – 2013-03-08 19:43:09

+0

哪个错误?名称 – Guru 2013-03-09 04:50:04

+0

错误是:行[[NSUserDefaults standardUserDefaults] synchronize]上的预期“]”; – 2013-03-09 19:03:44

0

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