0

在 cocos2d-x 3.0 中标签的大小是否有限制?我正在尝试创建“打字机”效果,当字符串为 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);
}

charSprite->setScale(0)在 45 个字符后死亡。有任何想法吗?

4

1 回答 1

1

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

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);
    }
}
于 2014-08-05T22:57:52.340 回答