2014-02-12 66 views
0

我一直試圖弄清楚如何更新分數。我有一個字符串的標籤,該得分的推移,但它不更新更新分數Cocos2d 3.0

這是標籤比分多數民衆贊成假設更新

score=0; 
CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"Verdana-Bold" fontSize:18.0f]; 
scorelabel.positionType = CCPositionTypeNormalized; 
scorelabel.color = [CCColor blackColor]; 
scorelabel.position = ccp(0.85f, 0.95f); // Top Right of screen 
[self addChild:scorelabel]; 

那麼這就是在得分後加入2個精靈

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair monsterCollision:(CCNode *)monster projectileCollision:(CCNode *)projectile { 
//Creating another sprite on the position the monster one was. 
CCSprite *explosion = [CCSprite spriteWithImageNamed:@"explosion.png"]; 
explosion.position = monster.position; 
[self addChild:explosion]; 
[[OALSimpleAudio sharedInstance] playEffect:@"exsound.mp3"]; 

CCActionDelay *delay = [CCActionDelay actionWithDuration:.0f]; 
CCActionFadeOut *fade = [CCActionFadeOut actionWithDuration:.4f]; 
[explosion runAction:[CCActionSequence actionWithArray:@[delay,fade]]]; 

[monster removeFromParent]; 
[projectile removeFromParent]; 

score++; 

return YES; 
} 

,並建議之間的碰撞上,因爲scoreLabel拒絕衝突已檢測

日以後更新我能怎麼更新ank you:D

+2

cclabelttf很慢,當串改變(夠慢的每次字符串修改通知口吃),對於經常更新標籤一個cclabelbmfont或cclabelatlas是更好的選擇 – LearnCocos2D

回答

0

要更新得分

@Implement(在最高層)

CCLabelTTF *scorelabel; 

標籤顯示的分數

score=0; 
scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"a2203.ttf" fontSize:18.0f]; 
scorelabel.positionType = CCPositionTypeNormalized; 
scorelabel.color = [CCColor blackColor]; 
scorelabel.position = ccp(0.85f, 0.95f); // Top Right of screen 
[self addChild:scorelabel]; 

score++; 

[scorelabel setString:[NSString stringWithFormat:@"score: %d",score]]; 
2

您需要更新scoreLabel更新分數的地方。

所以後,

score++; 

包括

[scorelabel setString:[NSString stringWithFormat:@"score: %d",score]]; 
+0

出現一個錯誤,說scorelabel未申報 – Crazycriss

+0

我想,scorelabel的範圍僅限於您聲明它的塊。在您使用scorelabel的類中,通過在此處聲明@implementation {CCLabelTTF * scorelabel}來增加它的範圍。同時更換CCLabelTTF * scorelabel = [CCLabelTTF ....與scorelabel = [CCLabelTTF .... –

+0

FINALLY:d非常感謝你 它不是 「與scorelable」 只是scorelabel 但非常感謝你 – Crazycriss