在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(); }
版权属于:东哥笔记 - DongGe.org
本文链接:https://dongge.org/blog/188.html
自2017年12月26日起,『转载以及大段采集进行后续编辑』须注明本文标题和链接!否则禁止所有转载和采集行为!