cocos2d-x icon indicating copy to clipboard operation
cocos2d-x copied to clipboard

CCControlSwitch::create without labels will crash

Open ghost opened this issue 11 years ago • 1 comments

CCControlSwitch's internal methods assign a nullptr to Label* onLabel, Label* offLabel, which will crash when trying to add to the ClippingNode on line 196

clipper->addChild(onLabel);
clipper->addChild(offLabel);

ghost avatar Nov 27 '14 04:11 ghost

ControlSwitch* ControlSwitch::create
(Label* label, Label* onLabel, Label* offLabel, Scale9Sprite* switchSprite, MenuItem* thumb, MenuItem* onThumb, MenuItem* offThumb)
{
    ControlSwitch *pRet = new ControlSwitch();
    pRet->initWithMaskSprite(switchSprite, thumb, onThumb, offThumb);
    pRet->autorelease();

    if (label)
    {
        pRet->addLabel(label);
        label->release();
        // Set the labels' text color based on the switch's state
        if (pRet->isOn())
        {
            label->setColor(pRet->getOnTitleColor());
        }
        else
        {
            label->setColor(pRet->getOffTitleColor());
        }
    }

    if (onLabel)
    {
        pRet->addLabel(onLabel);
        onLabel->release();
        onLabel->setVisible(pRet->isOn());
    }

    if (offLabel)
    {
        pRet->addLabel(offLabel);
        offLabel->release();
        offLabel->setVisible(!pRet->isOn());
    }

    return pRet;
}

ljluestc avatar Sep 17 '23 23:09 ljluestc