今天在看到CCAction的继承关系中,有一个CCCallFunc类,其下有三个子类:
CCCallFuncN
CCCallFuncND
CCCallFuncO
于是研究了下这三个类,回想起之前CCObject类中有这样一段代码:
typedef void (CCObject::*SEL_SCHEDULE)(float);
typedef void (CCObject::*SEL_CallFunc)();
typedef void (CCObject::*SEL_CallFuncN)(CCNode*);
typedef void (CCObject::*SEL_CallFuncND)(CCNode*,void*);
typedef void (CCObject::*SEL_CallFuncO)(CCObject*);
typedef void (CCObject::*SEL_MenuHandler)(CCObject*);
typedef void (CCObject::*SEL_EventHandler)(CCEvent*);
typedef int (CCObject::*SEL_Compare)(CCObject*);
#define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)
#define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR)
#define callfuncN_selector(_SELECTOR) (SEL_CallFuncN)(&_SELECTOR)
#define callfuncND_selector(_SELECTOR) (SEL_CallFuncND)(&_SELECTOR)
#define callfuncO_selector(_SELECTOR) (SEL_CallFuncO)(&_SELECTOR)
#define menu_selector(_SELECTOR) (SEL_MenuHandler)(&_SELECTOR)
#define event_selector(_SELECTOR) (SEL_EventHandler)(&_SELECTOR)
#define compare_selector(_SELECTOR) (SEL_Compare)(&_SELECTOR)
于是觉得这两者之前是联系起来的。所以以下是用法:
在头文件中声明需要回调的函数,针对三个回调的参数不同,需要实现不同的函数:
void funCallBack();
void funCallBackN(CCNode *node);
void funCallBackND(CCNode *node,void *param);
void funCallBackO(CCObject *pSender);
然后在实现文件cpp中,定义CCAction:
CCSprite *sprite=CCSprite::create("Icon.png");
this->addChild(sprite);
CCCallFunc *funC=CCCallFunc::create(this,callfunc_selector(HelloWorld::funCallBack));
sprite->runAction(funC);
CCCallFuncN *funN=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::funCallBackN));
sprite->runAction(funN);
int i=8;
CCCallFuncND *funND=CCCallFuncND::create(this,callfuncND_selector(HelloWorld::funCallBackND),(void *)&i);
sprite->runAction(funND);
CCObject *obj=new CCObject();
CCCallFuncO *funO=CCCallFuncO::create(this,callfuncO_selector(HelloWorld::funCallBackO),obj);
sprite->runAction(funO);
void HelloWorld::funCallBack() { CCLOG("funCcallBack Ok"); } void HelloWorld::funCallBackN(CCNode *node) { CCLOG("funNcallBack Ok"); } void HelloWorld::funCallBackND(CCNode *node,void *param) { CCLOG("%d",param); CCLOG("funNDcallBack Ok"); } void HelloWorld::funCallBackO(CCObject *pSender) { if(pSender) { CCLOG("non NULL"); } CCLOG("funOcallBack Ok"); }