
方法一:
cocos2d-x 已经为我们封装好了,方法就在类cclabelTTF里面。
voID enableShadow(const CCSize &shadowOffset,float shadowOpacity,float shadowBlur,bool mustUpdateTexture = true); /** disable shadow rendering */ voID disableShadow(bool mustUpdateTexture = true); /** enable or disable stroke */ voID enablestroke(const cccolor3B &strokecolor,float strokeSize,bool mustUpdateTexture = true); /** disable stroke */ voID disablestroke(bool mustUpdateTexture = true);
方法二:
在方法一达不到效果的情况下就只能自己实现了。
.h文件
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; using namespace std; class HelloWorld : public cclayer { public: virtual bool init(); static CCScene* scene(); CREATE_FUNC(HelloWorld); //给文字添加描边 cclabelTTF* textAddstroke(const char* string,const char* Fontname,float FontSize,const cccolor3B &color3,float linewidth); //添加阴影 cclabelTTF* textAddShadow(const char* string,float shadowSize,float shadowOpacity); //既添加描边又添加阴影 cclabelTTF* textAddOutlineAndShadow(const char* string,float linewidth,float shadowOpacity); }; #endif // __HELLOWORLD_SCENE_H__ .cpp文件
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; CCScene* HelloWorld::scene() { CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !cclayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); //创建个背景 cclayercolor* whiteLayer = cclayercolor::create(ccc4(0,205,255),size.wIDth,size.height); this->addChild(whiteLayer); //创建描边 cclabelTTF* outline = textAddstroke("描边 stroke","Arial",40,ccwHITE,1); outline->setposition(ccp(size.wIDth*0.5,size.height*0.7)); this->addChild(outline); //创建阴影 cclabelTTF* shadow = textAddShadow("阴影 Shadow",2,200); shadow->setposition(ccp(size.wIDth*0.5,size.height*0.5)); this->addChild(shadow); //创建描边加阴影 cclabelTTF* outlineshadow = textAddOutlineAndShadow("描边加阴影 Outlineshadow",1,4,200); outlineshadow->setposition(ccp(size.wIDth*0.5,size.height*0.3)); this->addChild(outlineshadow); return true; } /* 制作文字描边效果是很简单的,我们写好一段文字之后,也就是创建出一个cclabelTTF,称之为正文cclabelTTF。然后再创建出4个cclabelTTF,颜色为黑色,大小同正文cclabelTTF相同, 称之为描边cclabelTTF。说到这大家可能已经明白了,没错,就是把4个描边cclabelTTF放于正文cclabelTTF的下面,分别于左右上下与正文cclabelTTF错开,这样描边效果就实现啦。。 *string 文本 *Fontname 文本字体类型 *FontSize 文本大小 *color3 文本颜色 *linewidth 所描边的宽度 */ cclabelTTF* HelloWorld::textAddstroke(const char* string,float linewidth) { //正文cclabelTTF cclabelTTF* center = cclabelTTF::create(string,Fontname,FontSize); center->setcolor(color3); //描边cclabelTTF 上 cclabelTTF* up = cclabelTTF::create(string,FontSize); up->setcolor(ccBLACK); up->setposition(ccp(center->getContentSize().wIDth*0.5,center->getContentSize().height*0.5+linewidth)); center->addChild(up,-1); //描边cclabelTTF 下 cclabelTTF* down = cclabelTTF::create(string,FontSize); down->setcolor(ccBLACK); down->setposition(ccp(center->getContentSize().wIDth*0.5,center->getContentSize().height*0.5-linewidth)); center->addChild(down,-1); //描边cclabelTTF 左 cclabelTTF* left = cclabelTTF::create(string,FontSize); left->setposition(ccp(center->getContentSize().wIDth*0.5-linewidth,center->getContentSize().height*0.5)); left->setcolor(ccBLACK); center->addChild(left,-1); //描边cclabelTTF 右 cclabelTTF* right = cclabelTTF::create(string,FontSize); right->setcolor(ccBLACK); right->setposition(ccp(center->getContentSize().wIDth*0.5+linewidth,center->getContentSize().height*0.5)); center->addChild(right,-1); return center; } /* 给文字添加阴影,一看就懂的。。。 *string 文本 *Fontname 文本字体类型 *FontSize 文本大小 *color3 文本颜色 *shadowSize 阴影大小 *shadowOpacity 阴影透明度 */ cclabelTTF* HelloWorld::textAddShadow(const char* string,float shadowOpacity) { cclabelTTF* shadow = cclabelTTF::create(string,FontSize); shadow->setcolor(ccBLACK); shadow->setopacity(shadowOpacity); cclabelTTF* center = cclabelTTF::create(string,FontSize); center->setcolor(color3); center->setposition(ccp(shadow->getContentSize().wIDth*0.5-shadowSize,shadow->getContentSize().height*0.5+shadowSize)); shadow->addChild(center); return shadow; } //既添加描边又添加阴影 cclabelTTF* HelloWorld::textAddOutlineAndShadow(const char* string,float shadowOpacity) { cclabelTTF* center = textAddstroke(string,FontSize,color3,linewidth); cclabelTTF* shadow = cclabelTTF::create(string,FontSize); shadow->setposition(ccp(center->getContentSize().wIDth*0.5+shadowSize,center->getContentSize().height*0.5-shadowSize)); shadow->setcolor(ccBLACK); shadow->setopacity(shadowOpacity); center->addChild(shadow,-1); return center; } 效果如图:
总结
以上是内存溢出为你收集整理的cocos2d-x 利用CCLabelTTF制作文字描边与阴影效果的实现方法全部内容,希望文章能够帮你解决cocos2d-x 利用CCLabelTTF制作文字描边与阴影效果的实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)