2013-03-08 72 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類來存儲高分。 希望這會有所幫助