近日,由于自己需要用到一个特效道具,show完特效后会删除场上几个同一类的精灵,为避免同时使用这两个道具时,产生冲突,导致前一个特效的精灵未被删除,所以想了个自定义回调函数的方法,众所周知,定义一个回调函数的用法大致是:
__String* draw = __String::create("1"); draw->retain(); for (auto sprite : _sp) { i++; auto time=DelayTime::create(0.002f * i); auto call = __CCCallFuncND::create(this,callfuncND_selector(gameLayer::showline),draw);//callfuncND_selector算是一个比较常见的,传递一个voID类型的参数 sprite->setTag((int)_sp.size()); sprite->runAction(Sequence::create(time,call,NulL)); }
但是翻遍底层逻辑,回调函数里边并不支持别的类型,转到callfuncND_selector的定义处,我们可以清楚的看到:
class Node;typedef voID (Ref::*SEL_CallFunc)();typedef voID (Ref::*SEL_CallFuncN)(Node*);typedef voID (Ref::*SEL_CallFuncND)(Node*,voID*);typedef voID (Ref::*SEL_CallFuncO)(Ref*);typedef voID (Ref::*SEL_MenuHandler)(Ref*);typedef voID (Ref::*SEL_SCHEDulE)(float);#define callfunc_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFunc>(&_SELECTOR)#define callfuncN_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncN>(&_SELECTOR)#define callfuncND_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncND>(&_SELECTOR)#define callfuncO_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncO>(&_SELECTOR)#define menu_selector(_SELECTOR) static_cast<cocos2d::SEL_MenuHandler>(&_SELECTOR)#define schedule_selector(_SELECTOR) static_cast<cocos2d::SEL_SCHEDulE>(&_SELECTOR)
其中只支持voID,node,ref以及float等类型,而我现在需要一个能够传递Vector的回调函数,所以只有自己重写一个了,因为正好callfuncND_selector满足我需要的格式,传递一个psender,以及一个voID类型的函数,所以可以抄袭这个类:
class CC_DLL __CCCallFuncND : public CallFunc{public: /** creates the action with the callback and the data to pass as an argument */ CC_DEPRECATED_ATTRIBUTE static __CCCallFuncND * create(Ref* target,SEL_CallFuncND selector,voID* d); // // OverrIDes // virtual __CCCallFuncND* clone() const overrIDe; virtual voID execute() overrIDe; protected: __CCCallFuncND() {} virtual ~__CCCallFuncND() {} /** initializes the action with the callback and the data to pass as an argument */ bool initWithTarget(Ref* target,voID* d); SEL_CallFuncND _callFuncND; voID* _data;private: CC_disALLOW_copY_AND_ASSIGN(__CCCallFuncND);};
将自己需要的回调函数重写:
//重写动作回调函数typedef voID (Ref::*ZYFun)(Node*,Vector<Mysprite*>);//将ZYFun声明为带node,和一个容器的函数,其中Mysprite是我自定义的类,继承自Sprite#define zy_selector(_SELECTOR) static_cast<::ZYFun>(&_SELECTOR)//再宏定义zy_selector回调形式class CC_DLL __ZYCallFuncND : public CallFunc //重写回调函数的类,取名叫__ZYcallFuncND类似<span >__CCCallFuncND</span>{public:// CC_DEPRECATED_ATTRIBUTE static __ZYCallFuncND * create(Ref* target,ZYFun selector,Vector<Mysprite*> d);//将参数换成自己的,传递的内容换成自定义的容器形式 virtual __ZYCallFuncND* clone() const overrIDe; virtual voID execute() overrIDe; protected: __ZYCallFuncND() {} virtual ~__ZYCallFuncND() {} /** initializes the action with the callback and the data to pass as an argument */ bool initWithTarget(Ref* target,Vector<Mysprite*> d); ZYFun _callFuncND; Vector<Mysprite*> _data; private: CC_disALLOW_copY_AND_ASSIGN(__ZYCallFuncND);};再看下cpp文件:
//__ZYCallFuncND自定义的回调函数__ZYCallFuncND * __ZYCallFuncND::create(Ref* target,Vector<Mysprite*> d){ __ZYCallFuncND* ret = new __ZYCallFuncND(); if (ret && ret->initWithTarget(target,selector,d)) { ret->autorelease(); return ret; } CC_SAFE_DELETE(ret); return nullptr;}bool __ZYCallFuncND::initWithTarget(Ref* target,Vector<Mysprite*> d){ if (CallFunc::initWithTarget(target)) { _data = d; _callFuncND = selector; return true; } return false;}voID __ZYCallFuncND::execute(){ if (_callFuncND) { (_selectorTarget->*_callFuncND)(_target,_data); }}__ZYCallFuncND * __ZYCallFuncND::clone() const{ // no copy constructor auto a = new __ZYCallFuncND(); if( _selectorTarget) { a->initWithTarget(_selectorTarget,_callFuncND,_data); } a->autorelease(); return a;}
最后你就可以使用自己的回调函数形式,顺利的传入自己的容器了:
if (_spp.size()>0) { auto call = __ZYCallFuncND::create(this,zy_selector(gameLayer::boomefover),_spp); auto timee=DelayTime::create(0.3); this->runAction(Sequence::create(timee,NulL)); }总结
以上是内存溢出为你收集整理的cocos2dx3.0自定义回调函数,传递自己想要的参数全部内容,希望文章能够帮你解决cocos2dx3.0自定义回调函数,传递自己想要的参数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)