0

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?

SpellChucker
  • 450
  • 5
  • 18
  • why are you setting the scale to 0? – GameDeveloper Aug 05 '14 at 14:10
  • To create a typewriter effect. I initially set all the letters to 0 so that I can run the action of setting their scale back to 1 to emulate a typewriter. – SpellChucker Aug 05 '14 at 15:00
  • I understand, ok. And just to be sure, it isn't actually crashing here: `textLabel->getLetter(i);` have you placed a break point and stepped over each line by line. – GameDeveloper Aug 05 '14 at 20:35
  • No it is definitely crashing on `charSprite->setScale(0)` on the 45th (44th with 0 index) character. Just stepped through the whole loop. – SpellChucker Aug 05 '14 at 21:02

1 Answers1

1

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);
    }
}
SpellChucker
  • 450
  • 5
  • 18