Cocos2d-x 3.0标签限制?

时间:2014-08-05 01:02:43

标签: label cocos2d-x cocos2d-x-3.0 ccaction

在cocos2d-x 3.0中,Label的大小是否有限制?我正在尝试创建一个'打字机'效果,它似乎在字符串不超过45个字符时起作用。如果通过EXC_BAD_ACCESS失败则更新。下面是我试图用来做这种打字机效果的代码:

const char *labelText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis elementum turpis nec erat auctor tempor. Aenean at lorem quis erat vehicula volutpat pretium in arcu. Nulla facilisi. Vestibulum ac nibh eros. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.";

// Set up the label.
auto textLabel = Label::createWithBMFont("CopperplateBold16.fnt",
                                      labelText,
                                      TextHAlignment::LEFT,
                                      textBacking->getContentSize().width - 200);
textLabel->setPosition(Vec2(origin.x + visibleSize.width * 0.5,
                              origin.y + visibleSize.height * 0.5));
this->addChild(textLabel, 2);

const int numChars = textLabel->getStringLength();
for (int i = 0; i < numChars; i++) {
    CCLOG("Char: %d", i);
    Sprite* charSprite = textLabel->getLetter(i);
    charSprite->setScale(0);

    float delaySec = (10.0/(numChars - 1)) * i;
    DelayTime *delay        = DelayTime::create(delaySec);
    ScaleTo *appear         = ScaleTo::create(0, 1);
    Sequence *delayAndShow  = Sequence::create(delay, appear, NULL);

    charSprite->runAction(delayAndShow);
}

在45个字符后,charSprite->setScale(0)就会死亡。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是我想的无效字符。解决这个问题的方法是检查sprite的有效性:

for (int i = 0; i < numChars; i++) {
    auto charSprite = textLabel->getLetter(i);

    if (charSprite) {
        charSprite->setScale(0);

        float delaySec = (10.0/(numChars - 1)) * i;
        DelayTime *delay        = DelayTime::create(delaySec);
        ScaleTo *appear         = ScaleTo::create(0, 1);
        Sequence *delayAndShow  = Sequence::create(delay, appear, NULL);

        charSprite->runAction(delayAndShow);
    }
}