2014-02-11 71 views
0

我如何編碼碰撞計數?現在,它可以顯示爲分數:d計數碰撞Cocos2d 3.0

這是我撞的代碼

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

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

[monster removeFromParent]; 
[projectile removeFromParent]; 
return YES; 

} 

,我會添加什麼來算怪物的碰撞?

謝謝:)

新代碼

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

回答

1

聲明的類屬性來計算碰撞的時代。

int score; 

然後在你的init方法將其設置爲零

int score=0; 

最後每一次碰撞由一個如發生增量的值。

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

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

[monster removeFromParent]; 
[projectile removeFromParent]; 
return YES; 

} 
+0

好吧,所以我得到了所有的工作,但現在我試圖顯示它和標籤,我不會更新。我更新了上面的代碼,並感謝您的所有建議:D – Crazycriss

+0

如果這是其他問題,我會感謝您的正確答案檢查,然後您可以再次詢問其他主題,我可以再次幫助您。謝謝 –