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

James Webster
  • 31,873
  • 11
  • 70
  • 114
Crazycriss
  • 315
  • 1
  • 5
  • 19
  • 2
    cclabelttf is very slow when the string changes (slow enough to notice a stutter every time the string changes), for frequently updating labels A cclabelbmfont or cclabelatlas are better choices – CodeSmile Feb 12 '14 at 08:49

2 Answers2

2

You need to update the scoreLabel where you update score.

So after ,

 score++;

Include

[scorelabel setString:[NSString stringWithFormat:@"score: %d",score]];
Abhineet Prasad
  • 1,271
  • 2
  • 11
  • 14
  • I guess, the scope of scorelabel is restricted to the block where you declared it. In the class where you are using the scorelabel ,increase its scope by declaring it here @implementation{ CCLabelTTF* scorelabel} . Also replace CCLabelTTF *scorelabel = [CCLabelTTF.... with scorelabel = [CCLabelTTF.... – Abhineet Prasad Feb 13 '14 at 06:25
  • FINALLY :D thank you very much its not the "with scorelable" just scorelabel but thank you very much – Crazycriss Feb 13 '14 at 07:07
0

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]];
Crazycriss
  • 315
  • 1
  • 5
  • 19