Labels in cocos2d

This tutorial will show you how to initialize a Label, change its position, change its color, and change what is displayed in the label.

NSString *name;
int _level;
int eggs;
int _score;
// create and initialize a Label
CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"ShowG.TTF" fontSize:20];

// position the label on the top left of the screen
label.position =  ccp( size.width /2 , 250);
//Changes the color of the label
label.color = ccc3(255,150,58);

// add the label as a child to this Layer
[self addChild: label];
//Change the text of the label
[label setString:[NSString stringWithFormat:@"Name: %@, Level: %d, Eggs: %d, Score: %d", name,_level,eggs,_score]];

When you add values to labels, %@ is used for strings, and %d is used for numbers. Remember this our you will get a garbage value. That is how you use labels with cocos2d if you have any questions comment bellow.

Copy right Britney Lee Johnson September 2010.

This entry was posted in cocos2d. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>