CCCallFunc * CCCallFunc::actionWithTarget(SelectorProtocol* pSelectorTarget,SEL_CallFunc selector); CCCallFuncN * CCCallFuncN::actionWithTarget(SelectorProtocol* pSelectorTarget,SEL_CallFuncN selector); CCCallFuncND * CCCallFuncND::actionWithTarget(SelectorProtocol* pSelectorTarget,SEL_CallFuncND selector, void* d); CCCallFuncO * CCCallFuncO::actionWithTarget(SelectorProtocol* pSelectorTarget,SEL_CallFuncO selector, CCObject* pObject)
typedef void (SelectorProtocol::*SEL_CallFunc)(); typedef void (SelectorProtocol::*SEL_CallFuncN)(CCNode*); typedef void (SelectorProtocol::*SEL_CallFuncND)(CCNode*, void*); typedef void (SelectorProtocol::*SEL_CallFuncO)(CCObject*);最终,我们写出来的代码看起来像是这样的:
CCAction *a1=CCCallFunc::actionWithTarget(this, callfunc_selector(A::f1)); this->runAction(a1); CCAction *a2=CCCallFuncN::actionWithTarget(this, callfuncN_selector(A::f2)); this->runAction(a2); int i; CCAction *a3=CCCallFuncND::actionWithTarget(this, callfuncND_selector(A::f3),(void*)&i); this->runAction(a3); CCObject *obj; CCAction *a4=CCCallFuncO::actionWithTarget(this, callfuncO_selector(A::f4),obj); this->runAction(a4);
CCCallFunc作为一个CCAction可以用来调用自己的函数。其用处还是很多的。
1、用一个不带参数的函数给一个action创建回调功能。使用CCCallFunc
CCFiniteTimeAction* actions= CCSequence::actions(action, CCCallFunc::actionWithTarget(this,callfunc_selector(MyClass::callBackAnim)),NULL);这就创建了一个带回调函数的action,当这个action执行完毕后,就会执行callBackAnim函数。这里的 callBackAnim是MyClass里面的不带参数的函数。最后要加上一个NULL,因为CCSequence相当于一个队列,它会按顺序执行里面的所有action,最后一个加NULL。表示到这里就没有了。
2、用一个带参数的函数给一个action创建回调功能。使用CCCallFuncND
回调函数为callBackAnim(CCNode* sender,int myInt);
CCFiniteTimeAction* actions=CCSequence::actions(action,CCCallFuncND::actionWithTarget(this,
callfuncND_selector(MyClass::callBackAnim), (void*)mInt),NULL);
3、CCCallFuncN
回调函数为callBackAnim(CCNode* sender);
CCFiniteTimeAction* actions=CCSequence::actions(action,
CCCallFuncN::actionWithTarget(this, callfuncN_selector(MyClass::callBackAnim)),NULL);
这个就是不传递参数,但是把谁run这个action的对象传递过去了。
4、CCCallFuncO
回调函数为callBackAnim(CCObject* obj);
参考:http://blog.csdn.net/yanghuiliu/article/details/6933277
http://4137613.blog.51cto.com/4127613/762321