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




☟☟可点击下方广告支持一下☟☟

最后修改:1970 年 01 月 01 日
请我喝杯可乐,请随意打赏: ☞已打赏列表