cocos2dx lablettf 描边错乱

2015年01月18日 11:43 0 点赞 0 评论 更新于 2025-11-21 14:43

在使用 Cocos2d-x 进行开发时,我遇到了 Cocos2d-x LabelTTF 描边错乱的问题。下面将详细介绍该问题的表现、相关代码以及解决办法。

问题表现与出错代码

以下是出现描边错乱问题的代码:

function createTextFieldBySize(str_label, width, font_size, x, y, point)
local label = cc.Label:createWithTTF(str_label, "fonts/chinese.ttf", font_size)
label:setTextColor(cc.c4b(0xFF, 0xFF, 0xFF, 0xFF))
label:enableOutline(cc.c4b(0x5F, 0x30, 0x1E, 0xFF), 2)
label:enableGlow(cc.c4b(1, 42, 67, 255))
if point ~= nil then
label:setAnchorPoint(point)
end
if width ~= nil then
local label_width = label:getContentSize().width
local label_height = label:getContentSize().height
if label_width > width then
local line_num = math.ceil(label_width/width)
label:setContentSize(cc.size(width, label_height*line_num))
label:setWidth(width)
-- label:setHeight(label_height*line_num)
end
end
if x ~= nil and y ~= nil then
label:setPosition(cc.p(x, y))
end
return label
end

当运行上述代码时,会出现 LabelTTF 描边错乱的情况。经过测试发现,将 label:enableOutline(cc.c4b(0x5F, 0x30, 0x1E, 0xFF), 2) 这一行代码去掉后,描边错乱的问题就不会出现。

网上找到的解决办法

初始化方法

在网上搜索时,有些朋友提出可以先对 Label 进行初始化,示例代码如下:

TTFConfig ttfConfig("PixelMplus12-Bold.ttf",20,GlyphCollection::DYNAMIC);
ttfConfig.customGlyphs = nullptr;
descripLabel = Label::createWithTTF(ttfConfig, textString);
descripLabel->setTextColor(Color4B(Color3B(0x33,0x33,0x33)));
descripLabel->enableOutline(Color4B(Color3B(0xff,0xff,0xff)),2.0);
descripLabel->setLineHeight(35.0f);
descripLabel->setDimensions(550, 100);

这种初始化方法在一定程度上可能会解决描边错乱的问题,但并非对所有情况都有效。

更换版本

经过实践验证,最有效的解决办法是将 Cocos2d-x 更换为 3.2 版本。在 3.2 版本中,该描边错乱的问题不会出现。

综上所述,如果你在使用 Cocos2d-x 时遇到 LabelTTF 描边错乱的问题,可以先尝试去掉 enableOutline 方法,或者采用初始化的方式。若问题仍然存在,不妨考虑更换为 3.2 版本。

作者信息

feifeila

feifeila

共发布了 3994 篇文章