我们在代码中一一注解
首先,CCLabelTTF 是我们常见的一种文本,这种文本由于渲染效率不高,有了CCLabelAltas。这种文本 渲染比较快,且比较灵活。
最终的版本是CCLabelIBMFont,这种支持不同宽度的字符。
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
// create and initialize a Label
CCLabelTTF *label1 = [CCLabelTTF labelWithString:@"We Love Apple" fontName:@"Marker Felt" fontSize:30];
CCLabelTTF *label2 = [CCLabelTTF labelWithString:@"we Love Ihpone" fontName:@"ArialRoundedMTBold" fontSize:32 dimensions:CGSizeMake(150, 150) hAlignment:UITextAlignmentLeft];
CCLabelTTF *label3 = [CCLabelTTF labelWithString:@"We Love Ipad" fontName:@"Chalkduster" fontSize:32 dimensions:CGSizeMake(150, 150) hAlignment:UITextAlignmentLeft lineBreakMode:kCCLineBreakModeWordWrap];
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label1.position = ccp( size.width /2 , size.height/2 );
label2.position = ccp( size.width /2 , 150+size.height/2 );
label3.position = ccp( size.width /2 , -150+size.height/2 );
// add the label as a child to this Layer
[self addChild: label1];
[self addChild: label2];
[self addChild: label3];
id label1Action = [CCSpawn actions:[CCScaleBy actionWithDuration:5 scale:1.3],[CCFadeIn actionWithDuration:5], nil];
[label1 runAction:label1Action];
id label2Action = [CCSpawn actions:[CCFadeIn actionWithDuration:5],[CCRotateBy actionWithDuration:5 angle:180], nil];
[label2 runAction:label2Action];
id label3Action = [CCSpawn actions:[CCFadeIn actionWithDuration:5],[CCRotateBy actionWithDuration:5 angle:180], nil];
[label3 runAction:label3Action];
//CCLabelTTF渲染比较慢,其替代类:CCLabelAtlas
CCLabelAtlas *label4 = [CCLabelAtlas labelWithString:@"123Test" charMapFile:@"tuffy_bold_italic-charmap-hd.png" itemWidth:48 itemHeight:64 startCharMap:32];
label4.position = ccp(size.width*0.25, size.height/2);
[self addChild:label4];
//CCLabelIBMFont 类支持不同宽度的字符,有很多第三方编辑器的支持,比CCLabelAtlas更加灵活
CCLabelBMFont *label5 = [CCLabelBMFont labelWithString:@"Hello" fntFile:@"myfont.fnt"];
label5.position = ccp(size.width/2, size.height/2);
[self addChild:label5];
id labelAction5 = [CCSpawn actions:[CCScaleBy actionWithDuration:2 scale:4],[CCFadeIn actionWithDuration:2], nil];
[label5 runAction:labelAction5];
//标签的对齐方式 依赖锚点 anchorPoint
//1 右对齐
label1.anchorPoint = ccp(0, 0.5);
//2左对齐
label2.anchorPoint = ccp(1, 0.5);
//
//
//顶部对齐
label3.anchorPoint = ccp(0.5, 0);
//底部对齐
label4.anchorPoint =ccp(0.5, 1);
//默认的几何中心位置
label5.anchorPoint = ccp(0.5, 0.5);
// Leaderboards and Achievements
//
// Default font size will be 28 points.
}
return self;
}
打飞机游戏中倒计时的关键代码:
CCSprite *char1 = (CCSprite *)[_countdownLlabel getChildByTag:0];
CCSprite *char4 = (CCSprite *)[_countdownLlabel getChildByTag:3];
id scaleTo = [CCScaleBy actionWithDuration:1 scale:5];
id scaleBack = [scaleTo reverse];
id seq = [CCSequence actions:scaleTo,scaleBack, nil];
id ac = [CCRepeatForever actionWithAction:seq];
[char1 runAction:ac];
[char4 runAction:[[ac copy] autorelease]];