quickV3 Label setColor()相关Bug

quickV3 Label setColor()相关Bug

在cocos2dx3.3中label在setString(text)后,当调用 getLetter(letterIndex) , 如果 letterIndex 比 text长度大, 就可能引起crash. 如果新设置的颜色文本比原来的的文本短,就会引起removeQuadAtIndex:Invalid index 在TextureAtlas.cpp中的removeQuadAtIndex(ssize_t index)中。
官方已经fix了这个bug 具体见issue:#11659、#11683 & #11728

主要是在CCLebel:alignText()中
“` void Label::alignText()
{
if (_fontAtlas == nullptr || _currentUTF16String.empty())
{
return;
}
/* 将这个移动到updateQuads()中,不然removeQuadAtIndex就会报错
- for (const auto& batchNode:_batchNodes)
- {
- batchNode->getTextureAtlas()->removeAllQuads();
- }*/

_fontAtlas->prepareLetterDefinitions(_currentUTF16String);
auto textures = _fontAtlas->getTextures();
if (textures.size() > _batchNodes.size())
{
    for (auto index = _batchNodes.size(); index < textures.size(); ++index)
    {
        auto batchNode = SpriteBatchNode::createWithTexture(textures[index]);
        batchNode->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
        batchNode->setPosition(Vec2::ZERO);
        Node::addChild(batchNode,0,Node::INVALID_TAG);
        _batchNodes.push_back(batchNode);
    }
}
// optimize for one-texture-only sceneario
// if multiple textures, then we should count how many chars
// are per texture
// add by Natsu
if (_batchNodes.size()==1) {
    _batchNodes.at(0)->reserveCapacity(_currentUTF16String.size());
}

LabelTextFormatter::createStringSprites(this);
if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) )
    LabelTextFormatter::createStringSprites(this);

if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT))
    LabelTextFormatter::alignText(this);

// cocos #11659


if (!_children.empty())
{

    int strLen = static_cast<int>(_currentUTF16String.length());
    Rect uvRect;
    Sprite* letterSprite;
    for (auto index = 0; index < _children.size();) {
        auto child = _children.at(index);
        int tag = child->getTag();

        if(tag >= strLen)
        {
            //SpriteBatchNode::removeChild(child, true);  //cocos2dx #11728
            child->removeFromParentAndCleanup(true);
        }
        else if(tag >= 0)

        {
            letterSprite = dynamic_cast<Sprite*>(child);
            if (letterSprite)
            {
                auto& letterDef = _lettersInfo[tag].def;
                uvRect.size.height = letterDef.height;
                uvRect.size.width  = letterDef.width;
                uvRect.origin.x    = letterDef.U;
                uvRect.origin.y    = letterDef.V;

                letterSprite->setTexture(textures[letterDef.textureID]);

                letterSprite->setTextureRect(uvRect, false, uvRect.size); //cocos2dx #11728


                //cocos2dx #11728

                letterSprite->setPosition(_lettersInfo[tag].position.x + letterDef.width/2,
                                          _lettersInfo[tag].position.y - letterDef.height/2);
            }
            ++index;
        }
        else
        {
            ++index;
        }
    }
}

updateQuads();

updateColor();

}“`

具体参考GitHub Files Changed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值