cocos2dx简单实现描边
发布时间:2020-12-14 19:57:47 所属栏目:百科 来源:网络整理
导读:版本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(cons
版本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);缺点:描边尺寸不能设置太大,否则会有问题。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |