cocos2d-x 3.0 为字体添加黑边和阴影

时间:2023-02-08 17:58:59

之前因项目需求要做一个为字体添加阴影和黑边的效果,看到了网上这位仁兄的博客http://blog.csdn.net/song_hui_xiang/article/details/17375279,自己实现了一遍这两种效果,我觉得这种最基本的需求coco2d-x应该有吧,结果在testCpp里搜了一下,果然有这种用法,以下是该结构体的成员

// font attributes
struct FontDefinition
{
public:
/**
* @js NA
* @lua NA
*/
FontDefinition()
: _fontSize(0)
, _alignment(TextHAlignment::CENTER)
, _vertAlignment(TextVAlignment::TOP)
, _dimensions(Size::ZERO)
, _fontFillColor(Color3B::WHITE)
{}

// font name
std::string _fontName;
// font size
int _fontSize;
// horizontal alignment
TextHAlignment _alignment;
// vertical alignment
TextVAlignment _vertAlignment;
// renering box
Size _dimensions;
// font color
Color3B _fontFillColor;
// font shadow
FontShadow _shadow;
// font stroke
FontStroke _stroke;

};
都是些很简单的注释,相信各位朋友应该能看懂

下面是我自己做的一个测试,效果还不错,不过还是建议看一下 song_hui_xiang的那篇博客,至少想法是值得学习的!

<pre name="code" class="cpp">    Size shadeOffSet(5.0,5.0);
//定义字体
FontDefinition TextDef;
TextDef._fontSize = 100;
TextDef._fontName = std::string("Arial");
TextDef._fontFillColor = Color3B::RED;
//定义阴影
TextDef._shadow._shadowEnabled = true;
TextDef._shadow._shadowOffset = shadeOffSet;
TextDef._shadow._shadowOpacity = 1.0f;
TextDef._shadow._shadowBlur = 0.5f;//模糊度
//定义黑边
TextDef._stroke._strokeEnabled = true;
TextDef._stroke._strokeColor = Color3B::BLACK;
TextDef._stroke._strokeSize = 1.5f;

//label
LabelTTF* fontShadow = LabelTTF::createWithFontDefinition("I have get a First Blood!", TextDef);
fontShadow->setPosition(Point(m_winSize.width / 2 + 250, m_winSize.height));
this->addChild(fontShadow);


 

效果图如下:

cocos2d-x 3.0 为字体添加黑边和阴影