cocos2dx简单实现描边

版本2.x

首先写一个类继承CCLabelTTF

#pragma once
#include "cocos2d.h"
namespace Game
{

	using namespace cocos2d;
	class LabelTTFStroke:public cocos2d::CCLabelTTF
	{
	public:
		LabelTTFStroke(void);
		~LabelTTFStroke(void);
		static LabelTTFStroke * create(const char *string, const char *fontName, float fontSize, float strokeSize=0, const cocos2d::ccColor3B & strokeColor=ccc3(0,0,0), cocos2d::CCTextAlignment hAlignment=kCCTextAlignmentCenter, cocos2d::CCVerticalTextAlignment vAlignment=kCCVerticalTextAlignmentTop);
		void visit(); 
	private:
		cocos2d::ccColor3B m_strokeColor;
		float m_strokeSize;
	};
}

#include "LabelTTFStroke.h"
namespace Game
{
	using namespace cocos2d;
LabelTTFStroke::LabelTTFStroke(void):
m_strokeColor(ccc3(0,0,0)),
m_strokeSize(0.0f)
{
}

LabelTTFStroke::~LabelTTFStroke(void)
{
}

void LabelTTFStroke::visit()
{
	if(!isVisible())
		return;
	if(m_strokeSize>0)
	{
		ccColor3B col = getColor();
		 CCPoint pos = getPosition();
		 setColor(m_strokeColor);
        setPosition(ccp(pos.x + 1 * m_strokeSize, pos.y + 1 * m_strokeSize));
        CCLabelTTF::visit();
        setPosition(ccp(pos.x - 1 * m_strokeSize, pos.y -1 *m_strokeSize));
        CCLabelTTF::visit();
		setPosition(ccp(pos.x + 1 * m_strokeSize, pos.y - 1 * m_strokeSize));
        CCLabelTTF::visit();
        setPosition(ccp(pos.x - 1 * m_strokeSize, pos.y + 1 * m_strokeSize));
        CCLabelTTF::visit();
		setColor(col);
        setPosition(ccp(pos.x, pos.y));
	}
	CCLabelTTF::visit();
}

LabelTTFStroke * LabelTTFStroke::create(const char *string, const char *fontName, float fontSize, float strokeSize, const cocos2d::ccColor3B & strokeColor, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
{
	LabelTTFStroke *pRet = new LabelTTFStroke();
	if(pRet && pRet->initWithString(string, fontName, fontSize, CCSizeZero, hAlignment, vAlignment))
    {
		pRet->m_strokeColor = strokeColor;
		pRet->m_strokeSize = strokeSize;
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}
}

重写visit()函数,不同的方向,根据描边的宽度,重新画4遍,这样一个描边就相当于画了5遍,描边4遍,自己一遍

visit 也可以这样写

void LabelTTFStroke::visit()
{
	if(!isVisible())
		return;
	if(m_strokeSize>0)
	{
		ccColor3B col = getColor();
		 CCPoint pos = getPosition();
		 setColor(m_strokeColor);
        setPosition(ccp(pos.x + 1 * m_strokeSize, pos.y));
        CCLabelTTF::visit();
        setPosition(ccp(pos.x - 1 * m_strokeSize, pos.y));
        CCLabelTTF::visit();
		setPosition(ccp(pos.x, pos.y - 1 * m_strokeSize));
        CCLabelTTF::visit();
        setPosition(ccp(pos.x, pos.y + 1 * m_strokeSize));
        CCLabelTTF::visit();
		setColor(col);
        setPosition(ccp(pos.x, pos.y));
	}
	CCLabelTTF::visit();
}

使用方法:

Game::LabelTTFStroke* pLabel1 = Game::LabelTTFStroke::create("Hello World", "Arial", 30, 2.0, ccc3(255,0,0));
    
    // position the label on the center of the screen
    pLabel1->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height-50));

    // add the label as a child to this layer
    this->addChild(pLabel1, 1);

缺点:描边尺寸不能设置太大,否则会有问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值