码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x 文本渲染

时间:2015-08-12 18:27:30      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

 文本渲染:

技术分享

 

 

CCLabelAtlas、CCLabelBMFont、CCLabelTTF类都是继承 CCLabelProtocol类,即能够使用系统字,也能够自己定义渲染字体。

CCLabelAtlas类使用图片作为文字的一种方式, 通过图片直接定义

CCLabelAtlas *label0 = CCLabelAtlas::create("ASDE test ", "tuffy_bold_italic-charmap.png", 46, 64, ‘ ‘);//參数顺序:要显示字符,图片路径, 字符宽度,字符高度, 起始字符
	label0->setPosition(ccp(100, 200));
	label0->setOpacity(200);
	this->addChild(label0);

	CCLabelAtlas *label2 = CCLabelAtlas::create("456789123", "tuffy_bold_italic-charmap.png", 46, 64, ‘ ‘);
	label2->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
	label2->setOpacity(200);
	this->addChild(label2);
	


	//文本闪烁动画
	CCActionInterval *ac = CCFadeOut::create(1.2f);
	CCActionInterval *ac2 = ac->reverse();
	label2->runAction(CCRepeatForever::create(CCSequence::create(ac, ac2, NULL)));

	//使用plist配置文件的描写叙述来定义,能够依据须要改动配置文件信息,包含图片路径、字符宽度高度、起始字符
	CCLabelAtlas* label1 = CCLabelAtlas::create("WER VBN", "./tuffy_bold_italic-charmap.plist");//參数顺序:要显示字符,plist文件路径
	label1->setPosition( ccp(10,100) );
	label1->setOpacity( 200 );
	this->addChild(label1);


CCLabelTTF 类是通过系统字实现字体标签

CCLabelTTF *ttf = CCLabelTTF::create("HELLO WORLD ", "Helvetica", 30, ccp(320, 30), kCCTextAlignmentLeft);//參数顺序:要显示字符。字库名称,字号。范围大小。对齐方式[kCCTextAlignmentLeft(左对齐) kCCTextAlignmentRight(右对齐) kCCTextAlignmentCenter(中心对齐)]
	ttf->setPosition(ccp(300, 400));
	this->addChild(ttf);


CCTextFieldTTF类输入框使用文字标签,继承CCLabelTTF类

CCTextFieldTTF *pTest = CCTextFieldTTF::textFieldWithPlaceHolder("<click hee for input>", "STHeitiTC-Light",40 );
	pTest->setPosition(ccp(300, 500));
	this->addChild(pTest);


CCLabelBMFont类中每一个字都是一个精灵,每一个字都能够定义动作,并支持FNT类型文件

CCLabelBMFont *label = CCLabelBMFont::create("Bitmap Font Atlas Xub", "fonts/bitmapFontTest.fnt");
	addChild(label);

	CCSize s = CCDirector::sharedDirector()->getWinSize();

	label->setPosition( ccp(s.width/2-200, s.height/2) );
	label->setAnchorPoint( ccp(0.5f, 0.5f) );


	CCSprite* BChar = (CCSprite*) label->getChildByTag(0);//获取第1字符‘B‘
	CCSprite* FChar = (CCSprite*) label->getChildByTag(7);//获取第7字符‘F‘
	CCSprite* AChar = (CCSprite*) label->getChildByTag(12);//获取第12字符‘A‘
	CCSprite* XChar = (CCSprite*) label->getChildByTag(18);//获取第12字符‘A‘

	//为字符创建动作
	CCActionInterval* rotate = CCRotateBy::create(2, 360);
	CCAction* rot_4ever = CCRepeatForever::create(rotate);

	CCActionInterval* scale = CCScaleBy::create(2, 1.5f);
	CCActionInterval* scale_back = scale->reverse();
	CCSequence* scale_seq = CCSequence::create(scale, scale_back,NULL);
	CCAction* scale_4ever = CCRepeatForever::create(scale_seq);

	CCActionInterval* jump = CCJumpBy::create(0.5f, CCPointZero, 60, 1);
	CCAction* jump_4ever = CCRepeatForever::create(jump);

	CCActionInterval* fade_out = CCFadeOut::create(1);
	CCActionInterval* fade_in = CCFadeIn::create(1);
	CCSequence* seq = CCSequence::create(fade_out, fade_in, NULL);
	CCAction* fade_4ever = CCRepeatForever::create(seq);

	CCActionInterval *by = CCMoveBy::create(1.0f, ccp(300, 300));
	CCActionInterval *by2 = by->reverse();
	CCSequence* seq_by = CCSequence::create(by, by2, NULL);
	CCAction *ac_by = CCRepeatForever::create(seq_by);


	BChar->runAction(rot_4ever);
	BChar->runAction(scale_4ever);
	FChar->runAction(jump_4ever);
	AChar->runAction(fade_4ever);
	XChar->runAction(ac_by);


 

 

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Cocos2d-x 文本渲染

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/4725014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!