游戏中经常会用到文字描边和阴影,当cocos2dx中并没有给我们提供,下面是我参考:点击打开链接(http://blog.csdn.net/song_hui_xiang/article/details/17375279)和点击打开链接(http://fengmm521.blog.163.com/blog/static/2509135820138943638777/)自己弄了一个,基本上是一模一样,不喜勿喷!代码如下:
思路:多个CCLabelTTF重叠在一起。
头文件
// // CLabel.h // XXX // // Created by user on 14-3-4. // // #ifndef __XXX__CLabel__ #define __XXX__CLabel__ #include <iostream> #include "cocos2d.h" USING_NS_CC; namespace CLabel{ /*使用示例 CCSize size = CCDirector::sharedDirector()->getWinSize(); //创建个背景 CCLayerColor* whiteLayer = CCLayerColor::create(ccc4(0, 205, 205, 255), size.width, size.height); this->addChild(whiteLayer); //创建描边 CCLabelTTF* outline = CLabel::textAddOutline("描边 Outline", "Arial", 40, ccWHITE, 1); outline->setPosition(ccp(size.width*0.5, size.height*0.7)); this->addChild(outline); //创建阴影 CCLabelTTF* shadow = CLabel::textAddShadow("阴影 Shadow", "Arial", 40, ccWHITE, 2, 200); shadow->setPosition(ccp(size.width*0.5, size.height*0.5)); this->addChild(shadow); //创建描边加阴影 CCLabelTTF* outlineShadow = CLabel::textAddOutlineAndShadow("描边加阴影 OutlineShadow", "Arial", 40, ccWHITE, 1, 4, 200); outlineShadow->setPosition(ccp(size.width*0.5, size.height*0.3)); this->addChild(outlineShadow); */ //************************************************************************** // 给文字添加描边 //************************************************************************** CCLabelTTF* textAddOutline(const char* string, // 显示的文字 const char* fontName, // 字体名称 float fontSize, // 字体大小 const ccColor3B &color3, // 字体颜色 float lineWidth); // 描边宽度 //************************************************************************** // 添加阴影 CCLabelTTF* textAddShadow(const char* string, // 显示的文字 const char* fontName, // 字体名称 float fontSize, // 字体大小 const ccColor3B &color3, // 字体颜色 float shadowSize, // 阴影宽度 float shadowOpacity); // 阴影透明度 //************************************************************************** //************************************************************************** // 既添加描边又添加阴影 //************************************************************************** CCLabelTTF* textAddOutlineAndShadow(const char* string, // 显示的文字 const char* fontName, // 字体名称 float fontSize, // 字体大小 const ccColor3B &color3,// 阴影颜色 float lineWidth, // 字体宽度 float shadowSize, // 阴影宽度 float shadowOpacity); // 阴影透明度 }; #endif /* defined(__XXX__CLabel__) */
// // CLabel.cpp // XXX // // Created by user on 14-3-4. // // #include "CLabel.h" namespace CLabel{ /* 制作文字描边效果是很简单的,我们写好一段文字之后,也就是创建出一个CCLabelTTF,称之为正文CCLabelTTF。然后再创建出4个CCLabelTTF,颜色为黑色,大小同正文CCLabelTTF相同, 称之为描边CCLabelTTF。说到这大家可能已经明白了,没错,就是把4个描边CCLabelTTF放于正文CCLabelTTF的下面,分别于左右上下与正文CCLabelTTF错开,这样描边效果就实现啦。。 *string 文本 *fontName 文本字体类型 *fontSize 文本大小 *color3 文本颜色 *lineWidth 所描边的宽度 */ CCLabelTTF* textAddOutline(const char* string, const char* fontName, float fontSize,const ccColor3B &color3,float lineWidth) { //描边CCLabelTTF 左 CCLabelTTF* left = CCLabelTTF::create(string, fontName, fontSize); left->setColor(ccBLACK); //描边CCLabelTTF 右 CCLabelTTF* right = CCLabelTTF::create(string, fontName, fontSize); right->setColor(ccBLACK); right->setPosition(ccp(left->getContentSize().width*0.5+lineWidth*2,left->getContentSize().height*0.5)); left->addChild(right); //描边CCLabelTTF 上 CCLabelTTF* up = CCLabelTTF::create(string, fontName, fontSize); up->setColor(ccBLACK); up->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5+lineWidth)); left->addChild(up); //描边CCLabelTTF 下 CCLabelTTF* down = CCLabelTTF::create(string, fontName, fontSize); down->setColor(ccBLACK); down->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5-lineWidth)); left->addChild(down); //正文CCLabelTTF CCLabelTTF* center = CCLabelTTF::create(string, fontName, fontSize); center->setColor(color3); center->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5)); left->addChild(center); return left; } /* 给文字添加阴影,一看就懂的。。。 *string 文本 *fontName 文本字体类型 *fontSize 文本大小 *color3 文本颜色 *shadowSize 阴影大小 *shadowOpacity 阴影透明度 */ CCLabelTTF* textAddShadow(const char* string, const char* fontName, float fontSize,const ccColor3B &color3,float shadowSize,float shadowOpacity) { CCLabelTTF* shadow = CCLabelTTF::create(string, fontName, fontSize); shadow->setColor(ccBLACK); shadow->setOpacity(shadowOpacity); CCLabelTTF* center = CCLabelTTF::create(string, fontName, 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* textAddOutlineAndShadow(const char* string, const char* fontName, float fontSize,const ccColor3B &color3,float lineWidth,float shadowSize,float shadowOpacity) { CCLabelTTF* shadow = CCLabelTTF::create(string, fontName, fontSize); shadow->setColor(ccBLACK); shadow->setOpacity(shadowOpacity); CCLabelTTF* left = CCLabelTTF::create(string, fontName, fontSize); left->setColor(ccBLACK); left->setPosition(ccp(shadow->getContentSize().width*0.5-shadowSize, shadow->getContentSize().height*0.5+shadowSize)); shadow->addChild(left); CCLabelTTF* right = CCLabelTTF::create(string, fontName, fontSize); right->setColor(ccBLACK); right->setPosition(ccp(left->getContentSize().width*0.5+lineWidth*2,left->getContentSize().height*0.5)); left->addChild(right); CCLabelTTF* up = CCLabelTTF::create(string, fontName, fontSize); up->setColor(ccBLACK); up->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5+lineWidth)); left->addChild(up); CCLabelTTF* down = CCLabelTTF::create(string, fontName, fontSize); down->setColor(ccBLACK); down->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5-lineWidth)); left->addChild(down); CCLabelTTF* center = CCLabelTTF::create(string, fontName, fontSize); center->setColor(color3); center->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5)); left->addChild(center); return shadow; } }
CCSize size = CCDirector::sharedDirector()->getWinSize(); //创建描边 CCLabelTTF* outline = CLabel::textAddOutline("描边 Outline", "Arial", 40, ccWHITE, 1); outline->setPosition(ccp(size.width*0.5, size.height*0.7)); this->addChild(outline); //创建阴影 CCLabelTTF* shadow = CLabel::textAddShadow("阴影 Shadow", "Arial", 40, ccWHITE, 2, 200); shadow->setPosition(ccp(size.width*0.5, size.height*0.5)); this->addChild(shadow); //创建描边加阴影 CCLabelTTF* outlineShadow = CLabel::textAddOutlineAndShadow("描边加阴影 OutlineShadow", "Arial", 40, ccWHITE, 1, 4, 200); outlineShadow->setPosition(ccp(size.width*0.5, size.height*0.3)); this->addChild(outlineShadow);
// 效果图
class CCStrokeLabel : public CCNode { public: bool init(); static CCStrokeLabel* create(CCLabelTTF* labelTTF,ccColor3B fullColor,ccColor3B StrokeColor,float strokeSize); float getStrokeSize(); void setStrokeSize(float strokeSize); private: CCSprite* m_sprite; CCLabelTTF* m_label; ccColor3B m_fullColor; ccColor3B m_StrokeColor; float m_strokeSize; };
CCStrokeLabel* CCStrokeLabel::create(CCLabelTTF* labelTTF,ccColor3B fullColor,ccColor3B StrokeColor,float strokeSize) { CCStrokeLabel* tmp = new CCStrokeLabel; tmp->autorelease(); tmp->m_label = labelTTF; tmp->m_fullColor = fullColor; tmp->m_StrokeColor = StrokeColor; tmp->m_strokeSize = strokeSize; tmp->init(); return tmp; } bool CCStrokeLabel::init() { float strokeSize = getStrokeSize(); CCSize textureSize = m_label->getContentSize(); textureSize.width += 2 * strokeSize; textureSize.height += 2 * strokeSize; glGetError(); CCRenderTexture *rt = CCRenderTexture::create( textureSize.width, textureSize.height); if(!rt) { CCLOG("CCStrokeLabel::init:rt == NULL"); addChild(m_label); return false; } m_label->setColor(m_fullColor); ccBlendFunc originalBlend = m_label->getBlendFunc(); ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE}; m_label->setBlendFunc(func); m_label->setAnchorPoint(ccp(0.5, 0.5)); rt->begin(); for(int i = 0; i < 360; i += 15) { float r = CC_DEGREES_TO_RADIANS(i); m_label->setPosition(ccp( textureSize.width * 0.5f + sin(r) * strokeSize, textureSize.height * 0.5f + cos(r) * strokeSize)); m_label->visit(); } m_label->setColor(m_StrokeColor); m_label->setBlendFunc(originalBlend); m_label->setPosition(ccp(textureSize.width * 0.5f, textureSize.height * 0.5f)); m_label->visit(); rt->end(); CCTexture2D *texture = rt->getSprite()->getTexture(); texture->setAliasTexParameters(); m_sprite = CCSprite::createWithTexture(rt->getSprite()->getTexture()); setContentSize(m_sprite->getContentSize()); m_sprite->setAnchorPoint(ccp(0, 0)); m_sprite->setPosition(ccp(0, 0)); ((CCSprite *)m_sprite)->setFlipY(true); addChild(m_sprite); return true; } float CCStrokeLabel::getStrokeSize() { return m_strokeSize; } void CCStrokeLabel::setStrokeSize(float strokeSize) { m_strokeSize = strokeSize; }
CCLabelTTF* ttf = CCLabelTTF::create("ttflable","Arial",50); CCStrokeLabel* ttfStroke = CCStrokeLabel::create(ttf,ccc3(255,0,0),ccc3(0,0,255),2.0f); ttfStroke->setPosition(ccp(250,250)); this->addChild(ttfStroke,2); CCLabelTTF* ttfx = CCLabelTTF::create("ttf label test white","Arial",50); CCStrokeLabel* ttfStrokeWhite = CCStrokeLabel::create(ttfx,ccc3(0,255,0),ccc3(255,255,255),4.0f); ttfStrokeWhite->setPosition(ccp(255,500)); this->addChild(ttfStrokeWhite,2);