ioscocos2d-iphonecclabelttf

Update Score Cocos2d 3.0


Ive been trying to figure out how to update the score. I have an label with a string that the score goes on but it doesn't update

This is the label with the score thats suppose to update

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

Then this is where the score is added after a collision between two sprites

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

And advise onto how i could update it because the scoreLabel refuses to update after a collision has been detected

Thank you :D


Solution

  • To update the score

    @Implement (at the very top)

    CCLabelTTF *scorelabel;
    

    Label displaying the Score

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

    After

    score++;
    

    Write

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