Friday, 31 May 2013

How to create a label and position it properly: cocos2d

Adding and positioning a label (CCLabel ) in cocos2d is very simple. You can do it with only a few lines of code, as below:
    // create and initialize a Label
    CCLabelTTF *labelName = [CCLabelTTF labelWithString:@"Hello World" 
                                               fontName:@"Marker Felt" 
                                               fontSize:64];

    // ask director the the window size
    CGSize size = [[CCDirector sharedDirector] winSize];

    // position the label on the center of the screen
    labelName.position =  ccp( size.width /2 , size.height/2 ); 
    // To add Label Color
    labelName.color = ccc3(255, 0, 0);

    // add the label as a child to this Layer
    [self addChild: labelName];

No comments:

Post a Comment