cocos2d-x 利用CCLabelTTF制作文字描边与阴影效果的实现方法

转自: http://blog.csdn.net/song_hui_xiang/article/details/17375279 点击打开链接  


HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Box2D/Box2D.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;

class HelloWorld : public CCLayer
{
public:
    virtual bool init();  
    static cocos2d::CCScene* scene();
    CREATE_FUNC(HelloWorld);

	//给文字描边
	CCLabelTTF* text_add_out_line( const char* str, 
									const char* fontName, 
									float fontSize, 
									const ccColor3B &color3, 
									float lineWidth );

	//添加阴影
	CCLabelTTF* text_add_shadow( const char* str, 
									const char* fontName, 
									float fontSize, 
									const ccColor3B &color3,
									float shadowSize, 
									float shadowOpacity );

	//既添加描边 又 添加阴影
	CCLabelTTF* text_add_out_line_and_shadow( const char* str,
												const char* fontName,
												float fontSize,
												const ccColor3B &color3,
												float lineWidth, 
												float shadowSize,
												float shadowOpacity );
};

#endif  // __HELLOWORLD_SCENE_H__



HelloWorldScene.cpp

#include "HelloWorldScene.h"
#include <windows.h>
using namespace cocos2d;


CCScene* HelloWorld::scene()
{
	CCScene * scene = NULL;
	do 
	{
		// 'scene' is an autorelease object
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		// 'layer' is an autorelease object
		HelloWorld *layer = HelloWorld::create();
		CC_BREAK_IF(! layer);

		// add layer as a child to scene
		scene->addChild(layer);
	} while (0);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{

	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init() );

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

		//创建一个背景
		CCLayerColor* whiteLayer = CCLayerColor::create( ccc4(0, 205, 205, 255), size.width, size.height);
		this->addChild(whiteLayer);

		//创建描边
		CCLabelTTF* out_line = text_add_out_line("Out Line", "Arial", 40, ccWHITE, 1);
		out_line->setPosition(ccp(size.width*0.5f, size.height*0.7f));
		this->addChild(out_line);

		//创建阴影
		CCLabelTTF* shadow = text_add_shadow("Shadow", "Arial", 40, ccWHITE, 2, 200);
		shadow->setPosition(ccp(size.width*0.5f, size.height*0.5f));
		this->addChild(shadow);

		//创建描边加阴影
		CCLabelTTF* out_line_add_shadow = text_add_out_line_and_shadow("Out Line Shadow", "Arial", 40, ccWHITE, 1, 4, 200);
		out_line_add_shadow->setPosition(ccp(size.width*0.5f, size.height*0.3f));
		this->addChild(out_line_add_shadow);

		bRet = true;

	} while (0);

	return bRet;
}

/* 
    制作文字描边效果是很简单的,我们写好一段文字之后,也就是创建出一个CCLabelTTF,称之为正文CCLabelTTF。然后再创建出4个CCLabelTTF,颜色为黑色,大小同正文CCLabelTTF相同, 
    称之为描边CCLabelTTF。说到这大家可能已经明白了,没错,就是把4个描边CCLabelTTF放于正文CCLabelTTF的下面,分别于左右上下与正文CCLabelTTF错开,这样描边效果就实现啦。。 
 
    *string     文本 
    *fontName   文本字体类型 
    *fontSize   文本大小 
    *color3     文本颜色 
    *lineWidth  所描边的宽度 
 */  
CCLabelTTF* HelloWorld::text_add_out_line( const char* str, const char* fontName, float fontSize, const ccColor3B &color3, float lineWidth )
{
	//描边CCLabelTTF 左  
	CCLabelTTF* left = CCLabelTTF::create(str, fontName, fontSize);  
	left->setColor(ccBLACK);  

	//描边CCLabelTTF 右  
	CCLabelTTF* right = CCLabelTTF::create(str, 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(str, 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(str, 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(str, 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* HelloWorld::text_add_shadow( const char* str, const char* fontName, float fontSize, const ccColor3B &color3, float shadowSize, float shadowOpacity )
{
	CCLabelTTF* shadow = CCLabelTTF::create(str, fontName, fontSize);  
	shadow->setColor(ccBLACK);  
	shadow->setOpacity(shadowOpacity);  

	CCLabelTTF* center = CCLabelTTF::create(str, 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* HelloWorld::text_add_out_line_and_shadow( const char* str, const char* fontName, float fontSize, const ccColor3B &color3, float lineWidth, float shadowSize, float shadowOpacity )
{
	CCLabelTTF* shadow = CCLabelTTF::create(str, fontName, fontSize);  
	shadow->setColor(ccBLACK);  
	shadow->setOpacity(shadowOpacity);  

	CCLabelTTF* left = CCLabelTTF::create(str, 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(str, 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(str, 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(str, 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(str, fontName, fontSize);  
	center->setColor(color3);  
	center->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5));  
	left->addChild(center);  

	return shadow;  
}





显示效果:




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值