Cocos2dx 学习笔记23 callfun_selector用法

前端之家收集整理的这篇文章主要介绍了Cocos2dx 学习笔记23 callfun_selector用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天在看到CCAction的继承关系中,有一个CCCallFunc类,其下有三个子类:@H_502_2@

CCCallFuncN
@H_502_2@

CCCallFuncND
@H_502_2@

CCCallFuncO
@H_502_2@

于是研究了下这三个类,回想起之前CCObject类中有这样一段代码:@H_502_2@

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)
@H_502_2@


@H_502_2@

于是觉得这两者之前是联系起来的。所以以下是用法:@H_502_2@

在头文件中声明需要回调的函数,针对三个回调的参数不同,需要实现不同的函数:@H_502_2@

void funCallBack();
@H_502_2@void funCallBackN(CCNode *node);
@H_502_2@void funCallBackND(CCNode *node,void *param);
@H_502_2@void funCallBackO(CCObject *pSender);
@H_502_2@


@H_502_2@

然后在实现文件cpp中,定义CCAction:@H_502_2@

CCSprite *sprite=CCSprite::create("Icon.png");
@H_502_2@this->addChild(sprite);

@H_502_2@CCCallFunc *funC=CCCallFunc::create(this,callfunc_selector(HelloWorld::funCallBack));
@H_502_2@sprite->runAction(funC);

@H_502_2@CCCallFuncN *funN=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::funCallBackN));
@H_502_2@sprite->runAction(funN);

@H_502_2@int i=8;
@H_502_2@CCCallFuncND *funND=CCCallFuncND::create(this,callfuncND_selector(HelloWorld::funCallBackND),(void *)&i);
@H_502_2@sprite->runAction(funND);

@H_502_2@CCObject *obj=new CCObject();
@H_502_2@CCCallFuncO *funO=CCCallFuncO::create(this,callfuncO_selector(HelloWorld::funCallBackO),obj);
@H_502_2@sprite->runAction(funO);
@H_502_2@


@H_502_2@

最后实现方法调用:@H_502_2@

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"); }@H_502_2@

原文链接:https://www.f2er.com/cocos2dx/346346.html

猜你在找的Cocos2d-x相关文章