[一位菜鸟的COCOS-2D编程之路]COCOS2D中的文本渲染系统和实现倒计时

我们在代码中一一注解

首先,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]];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值