2013-03-08 43 views
0

我試圖解釋更好的情況。打印得分和高分

的變量是:

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

謝謝!

回答

0

你的setScore方法肯定不會工作,因爲你設置了highScore = punteggio,if將永遠不會成立。嘗試:

-(void) setHighScore:(int) newScore { 
    if(newScore>highScore) highScore = newScore; 
} 

以及每次更新當前比分

-(void)aggiungiPunti 
{ 
    punteggio = punteggio +0001; 
    [labelPunteggio setString:[NSString stringWithFormat:@"%d", punteggio]]; 
    [self setHighScore:punteggio]; 
} 

這樣你的高分變量總是被設置,玩遊戲什麼的狀態。不要忘記在當前遊戲開始時將highScore設置爲當前highScore,您必須堅持到某個地方。

+0

謝謝你!我嘗試但不起作用... 我打印在init metod像這樣labelScore = [CCLabelTTF labelWithString:@「0000」fontName:@「Marker Felt」fontSize:13]; [self addChild:labelScore]; labelhighScore = [CCLabelTTF labelWithString:@「0」fontName:@「Marker Felt」fontSize:13]; [self addChild:labelhighScore]; 得分變化但高分始終爲0。 – 2013-03-09 13:18:50