ControlButton

不要多次addTargetWithActionForControlEvents

注意这个函数是add,而不是set

ControlButton* NodeUtil::createOrUpdateControlButton(ControlButton* controlButton,const char* normalBgSpriteFrameName,const char* selectedBgSpriteFrameName,float scale,const Size& contentSize,Ref* target, Control::Handler action)
{
    Scale9Sprite* btnDown = Scale9Sprite::createWithSpriteFrameName(normalBgSpriteFrameName);
    Scale9Sprite* btnNormal = Scale9Sprite::createWithSpriteFrameName(selectedBgSpriteFrameName);
    
    btnNormal->setScale(scale);
    btnDown->setScale(scale);
    
    if(!controlButton)
    {
        controlButton = ControlButton::create(btnNormal);
    }
    else
    {
        controlButton->setBackgroundSpriteForState(btnNormal, Control::State::NORMAL);
    }
    controlButton->setBackgroundSpriteForState(btnDown, Control::State::SELECTED);
    
    Size tempSize=contentSize;
    if(tempSize.equals(Size::ZERO))
    {
        tempSize=btnDown->getContentSize();
    }
    
    controlButton->setPreferredSize(tempSize);
    
    if (target && action)
    {
        controlButton->removeTargetWithActionForControlEvents(target, action, Control::EventType::TOUCH_UP_INSIDE);
        controlButton->addTargetWithActionForControlEvents(target, action, Control::EventType::TOUCH_UP_INSIDE);
    }

    return controlButton;
}

发表评论