cocos2dx简单实现描边

前端之家收集整理的这篇文章主要介绍了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),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)),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,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));
	}
	CCLabelTTF::visit();
}

使用方法
Game::LabelTTFStroke* pLabel1 = Game::LabelTTFStroke::create("Hello World","Arial",30,2.0,ccc3(255,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);
缺点:描边尺寸不能设置太大,否则会有问题。

猜你在找的Cocos2d-x相关文章