【问题标题】:Cocos2d-x 3.0 Label limit?Cocos2d-x 3.0 标签限制?
【发布时间】:2014-08-05 01:02:42
【问题描述】:

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

这会在 45 个字符后在 charSprite-&gt;setScale(0) 上消失。有什么想法吗?

【问题讨论】:

  • 为什么要将比例设置为 0?
  • 创建打字机效果。我最初将所有字母设置为 0,以便我可以运行将它们的比例设置回 1 以模拟打字机。
  • 我明白了,好的。并且可以肯定的是,它实际上并没有在这里崩溃:textLabel-&gt;getLetter(i); 你是否放置了一个断点并逐行遍历每一行。
  • 不,它肯定会在第 45 个字符(第 44 个索引为 0)上的 charSprite-&gt;setScale(0) 上崩溃。刚刚完成了整个循环。

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


【解决方案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-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多