版本2.x
#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);缺点:描边尺寸不能设置太大,否则会有问题。
原文地址:https://www.jb51.cc/cocos2dx/345147.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。