Crazycriss
Crazycriss

Reputation: 315

Adding Score Label Cocos2d 3.0

I have an int for my score:

int score=0;

I understand how to add to the score, I just don't know how to display it

this is what I have but I'm pretty sure its wrong:

CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:@"score" fontName:@"Verdana-Bold" fontSize:18.0f];

int score=0;
CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:@"score" fontName:@"Verdana-Bold" fontSize:18.0f];
[self addChild:scorelabel];

Any advice on how to display the score on the scene?

Thank you

Code now

int score=0;
CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"Verdana-Bold" fontSize:18.0f];  **The warning**
scorelabel.positionType = CCPositionTypeNormalized;
scorelabel.position = ccp(0.0f, 0.0f);
[self addChild:scorelabel];

backButton

CCButton *backButton = [CCButton buttonWithTitle:@"[ Menu ]" fontName:@"Verdana-Bold" fontSize:18.0f];
backButton.positionType = CCPositionTypeNormalized;
backButton.position = ccp(0.85f, 0.95f); // Top Right of screen
[backButton setTarget:self selector:@selector(onBackClicked:)];
[self addChild:backButton];

Upvotes: 0

Views: 1761

Answers (2)

DDM
DDM

Reputation: 1129

The last solution suggested by @connor is correct. I suspect You might have run into issues of overlapping-layers (I was facing the same too & understand that it's often hard to figure out).

@implementation HelloWorldScene {
  //...
  CCLabelTTF * _label;
}

- (id)init
{
  self = [super init];
  //...
  _button     = [CCSprite spriteWithImageNamed:@"[email protected]"];
  _label      = [CCLabelTTF labelWithString:@"A Button" fontName:@"Helvetica Neue" fontSize:14.0f];
  _button.position      = ccp( 100.0f, 100.0f);
  _label.position       = ccp( 100.0f, 100.0f);

  [self addChild:_button    ]; //make sure a button or any other layer goes before the label
  [self addChild:_label     ]; //label or any kind of text must be added last
}

Upvotes: 0

Connor
Connor

Reputation: 64644

You need to add the label to your scene. If you are in the init method of a scene, you can use this line:

[self addChild:scorelabel];

Also, your score label includes the text "score" but not the actual score. If you want to include the score, change the creation of the label to this:

CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"Verdana-Bold" fontSize:18.0f];

Upvotes: 1

Related Questions