Cocos2d-x实现文字颜色渐变

更改CCLabel文件的updateColor()函数中的顶点颜色即可,代码如下:

void Label::updateColor()
{
    if (_batchNodes.empty())
    {
        return;
    }

    Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity );

    // special opacity for premultiplied textures
    if (_isOpacityModifyRGB)
    {
        color4.r *= _displayedOpacity/255.0f;
        color4.g *= _displayedOpacity/255.0f;
        color4.b *= _displayedOpacity/255.0f;
    }


    Color4B endColor=Color4B::RED;

    // special opacity for premultiplied textures
    if (_isOpacityModifyRGB)
    {
        endColor.r *= _displayedOpacity / 255.0f;
        endColor.g *= _displayedOpacity / 255.0f;
        endColor.b *= _displayedOpacity / 255.0f;
    }

    cocos2d::TextureAtlas* textureAtlas;
    V3F_C4B_T2F_Quad *quads;
    for (auto&& batchNode:_batchNodes)
    {
        textureAtlas = batchNode->getTextureAtlas();
        quads = textureAtlas->getQuads();
        auto count = textureAtlas->getTotalQuads();

        for (int index = 0; index < count; ++index)
        {
            quads[index].bl.colors = color4;
            quads[index].br.colors = color4;
            quads[index].tl.colors = endColor;
            quads[index].tr.colors = endColor;
            textureAtlas->updateQuad(&quads[index], index);
        }
    }
}

效果图:

猜你喜欢

转载自blog.csdn.net/auccy/article/details/127648502