2010-04-30 96 views
2

我正在cocos2d中製作遊戲,因爲在更新分數時,舊分數值將在標籤上得到,新值將被覆蓋。我米使用下面的代碼來顯示比分,更新Cocos2d中的標籤值

LblScore = [CCLabel labelWithString:[NSString stringWithFormat:@"%d",score] 
         dimensions:CGSizeMake(100, 300) 
          alignment:UITextAlignmentCenter 
          fontName:@"Arial" 
          fontSize:32.0]; 

因爲這樣的分數值不顯示,所有事情變得集結起來,如果有任何的想法一個人如何更新新的成績?

回答

0

對於我的問題的解決方案是,我必須在 - (id)init方法中定義標籤聲明,從任何地方提供的值將不會覆蓋值。

我已經試過了,它的工作,但還是感謝所有提供誰我幫助

8

我不完全明白你在做什麼,因爲我看不到你的所有代碼。但是,我認爲,你想要的是這樣的:

在場景中的init:

// Both of these are class variables 
score = 0; 
LblScore = [CCLabel labelWithString:[NSString stringWithFormat:@"%d",score] dimensions:CGSizeMake(100, 300) alignment:UITextAlignmentCenter fontName:@"Arial" fontSize:32.0]; 

// Position the score, wherever you want it 
[LblScore setPosition: CGPointMake(300, 240)]; 

當你的分數變化:

score++ // Not really, but your score changes somehow... 
[LblScore setString: [NSString stringWithFormat:@"%d",score]]; 

這部分很可能是在setScore:changeScore:方法更改您的內部分數值並同時更改標籤。

+0

感謝好友的幫助,但我得到了解決。 – 2010-05-01 09:50:54