labelcocos2d-xcocos2d-x-3.0ccaction

Cocos2d-x 3.0 Label limit?


Is there a limit to how big a Label can be in cocos2d-x 3.0? I'm trying to create a 'typewriter' effect and it seems to work when the string is 45 characters or less. If it's any more if fails with the usual EXC_BAD_ACCESS. Below is the code I'm trying to use to do this typewriter effect:

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);
}

This dies on the charSprite->setScale(0) after 45 characters. Any ideas?


Solution

  • Problem was invalid characters I guess. Way to fix this is to check the sprite for validity:

    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);
        }
    }