cocos2dx简单实现描边
来源:互联网 发布:混血 知乎 编辑:程序博客网 时间:2024/05/16 12:49
版本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);
缺点:描边尺寸不能设置太大,否则会有问题。
0 0
- cocos2dx简单实现描边
- Cocos2dx 之 PhysicsHandler 简单实现
- cocos2dx 简单血量条实现
- cocos2dx简单弹窗实现
- cocos2dx简单实现TabBarView功能
- Cocos2dx实现简单猜拳游戏
- 一个简单观察者模式的实现 cocos2dx
- cocos2dx 3.1 实现简单的翻牌效果
- cocos2dx 3.1 实现简单的截屏
- cocos2dx实现简单卡牌翻转效果
- 使用cocos2dx-v3实现简单地图缩放
- cocos2dx中的实现简单的画板功能
- Cocos2dx标签页(TabControl/TabView)的简单实现
- Cocos2dx标签页(TabControl/TabView)的简单实现
- cocos2dx shader实现的简单滤镜 基于3.0版本
- Cocos2dx-v3.1简单的Android编译实现
- cocos2dx 利用遮罩层来实现地图的简单寻路
- cocos2dx 简单实现遮罩的两种方式
- pathon 中 str和repr区别
- Python 字典和列表陷阱
- Android开发小经验
- SQLite:注意查询条件中空格的影响
- 实验五 树和二叉树
- cocos2dx简单实现描边
- BOOST 1.56 简明配置方法,资源整理
- 自定义排序
- iOS 远程通知
- POJ2486---Apple Tree
- [Bug Fix]Messy Audio语音电话不清晰问题
- 浮点运算是怎么实现的?
- 我的博客开始更新
- C++句柄类