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