cocosBuilder关闭按钮调用removeFromParent()闪退

在cocosbuilder使用绑定了按钮,然后绑定关闭函数

SEL_CCControlHandler WishingTreeLayer::onResolveCCBCCControlSelector(CCObject *pTarget, const char * pSelectorName)
{
    CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "CloseBtn", WishingTreeLayer::CloseBtnCallBack);
    return NULL;
}
bool WishingTreeLayer::onAssignCCBMemberVariable(CCObject* pTarget, const char* pMemberVariableName, CCNode* pNode)
{
    CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "WishingTreeLabel", CCLabelTTF*, m_pWishingTreeLabel);
    CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "wtclosebtn", CCControlButton*, m_pCloseBtn);
    return false;
}

调用函数也是简单的removeFromParent()函数,但是一点击关闭按钮就闪退,最后找到原因

void WishingTreeLayer::CloseBtnCallBack()
{
    
//    if (this->getChildrenCount()!=0) {
//        this->removeAllChildren();
//    }
    CCScene *pScene = CCDirector::sharedDirector()->getRunningScene();
    if( dynamic_cast<MainCityScene*>(pScene) != NULL )
    {
        ((MainCityScene*)pScene)->m_pCityBGLayer->ReShowCityHeadStatusCallBack(0);
    }
    removeFromParent();
}

因为函数写的时候是这样的,而在绑定的时候,他内部定义其实是需要这样的格式,所以这里是函数格式对不上,调用出现错误

这个可以点击看内部的函数

#define CCB_SELECTORRESOLVER_CCCONTROL_GLUE(TARGET, SELECTORNAME, METHOD) if(pTarget == TARGET && strcmp(pSelectorName, SELECTORNAME) == 0) { \
    return cccontrol_selector(METHOD); \
}
//再进一步
typedef void (CCObject::*SEL_CCControlHandler)(CCObject*, CCControlEvent);

#define cccontrol_selector(_SELECTOR) (SEL_CCControlHandler)(&_SELECTOR)

解决方案就是修改一下函数的格式即可。

函数修改为

void WishingTreeLayer::CloseBtnCallBack(CCObject*, CCControlEvent)
{
    
//    if (this->getChildrenCount()!=0) {
//        this->removeAllChildren();
//    }
    CCScene *pScene = CCDirector::sharedDirector()->getRunningScene();
    if( dynamic_cast<MainCityScene*>(pScene) != NULL )
    {
        ((MainCityScene*)pScene)->m_pCityBGLayer->ReShowCityHeadStatusCallBack(0);
    }
    removeFromParent();
}



Last modification:January 1st, 1970 at 08:00 am
如果看了这个文章可以让你少加会班,可以请我喝杯可乐
已打赏名单
微信公众号

Leave a Comment