今天在看到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"); }
总结以上是内存溢出为你收集整理的Cocos2dx 学习笔记23 callfun_selector用法全部内容,希望文章能够帮你解决Cocos2dx 学习笔记23 callfun_selector用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)