CCCallFunc CCCallFuncN CCCallFuncND都用来创建带有回调函数的动作,区别主要在于回调函数是否带有参数 CCCallFunc是执行对应的回调函数,其中回调函数不可带参数。一般使用静态成员函数create创建实例,create声明如下: static CCCallFunc* create ( CCObject * pSelectorTarget, SEL_CallFunc selector ) 回调函数通过execute方法执行,CCCallFunc中的execute的实现如下: void CCCallFunc::execute() { if (m_pCallFunc) { (m_pSelectorTarget->*m_pCallFunc)(); } if (m_nScriptHandler) { CCScriptEngineManager::sharedManager()->getScriptEngine()->executeCallFuncActionEvent(this); } } 通过(m_pSelectorTarget->*m_pCallFunc)();可以看到回调函数不包含参数
CCCallFuncN也是执行对应的回调函数,其中回调函数带一个参数。一般使用静态成员函数create创建实例,create声明如下: static CCCallFuncN* create ( CCObject * pSelectorTarget, SEL_CallFuncN selector ) 回调函数通过execute方法执行,CCCallFuncN中的execute的实现如下: void CCCallFuncN::execute() { if (m_pCallFuncN) { (m_pSelectorTarget->*m_pCallFuncN)(m_pTarget); } if (m_nScriptHandler) { CCScriptEngineManager::sharedManager()->getScriptEngine()->executeCallFuncActionEvent(this, m_pTarget); } } 通过(m_pSelectorTarget->*m_pCallFuncN)(m_pTarget);可以看到回调函数包含一个参数。
CCCallFuncND也是执行对应的回调函数,其中回调函数可带两个参数。一般使用静态成员函数create创建实例,create声明如下: static CCCallFuncND* create ( CCObject * pSelectorTarget, SEL_CallFuncND selector, void * d ) 回调函数通过execute方法执行,CCCallFuncND中的execute的实现如下: void CCCallFuncND::execute() { if (m_pCallFuncND) { (m_pSelectorTarget->*m_pCallFuncND)(m_pTarget, m_pData); } } 通过(m_pSelectorTarget->*m_pCallFuncND)(m_pTarget, m_pData);可以看到回调函数包含两个参数。
testCallFunc.h中代码 class testCallFunc : public CCLayer { protected: CCSprite* sprite1; CCSprite* sprite2; CCSprite* sprite3; public: virtual void onEnter(); void callback1(); void callback2(CCNode* sender); void callback3(CCNode* sender, void* data); }; testCallFunc.cpp中代码 void testCallFunc::onEnter() { //CCCallFunc的使用 CCFiniteTimeAction* action = CCSequence::create( CCMoveBy::create(2, ccp(200,0)), CCCallFunc::create(this, callfunc_selector(testCallFunc::callback1)), NULL); //CCCallFuncN的使用 CCFiniteTimeAction* action2 = CCSequence::create( CCScaleBy::create(2 , 2), CCFadeOut::create(2), CCCallFuncN::create(this, callfuncN_selector(testCallFunc::callback2)), NULL); //CCCallFuncNC的使用 CCFiniteTimeAction* action3 = CCSequence::create( CCRotateBy::create(3 , 360), CCFadeOut::create(2), CCCallFuncND::create(this, callfuncND_selector(testCallFunc::callback3), (void*)0xbebabeba), NULL); sprite1->runAction(action); sprite2->runAction(action2); sprite3->runAction(action3); } void testCallFunc::callback1() { CCSize s = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF *label = CCLabelTTF::create("callback 1 called", "Marker Felt", 16); label->setPosition(ccp( s.width/4*1,s.height/2)); addChild(label); } void testCallFunc::callback2(CCNode* pSender) { CCSize s = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF *label = CCLabelTTF::create("callback 2 called", "Marker Felt", 16); label->setPosition(ccp( s.width/4*2,s.height/2)); addChild(label); } void testCallFunc::callback3(CCNode* pTarget, void* data) { CCSize s = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF *label = CCLabelTTF::create("callback 3 called", "Marker Felt", 16); label->setPosition(ccp( s.width/4*3,s.height/2)); addChild(label); }
首先看一张图片,表示了CCCallFunc的继承关系: 1、CCCallFunc,回调,调用不带参数的函数: [cpp] CCSequence::actions(CCCallFunc::actionWithTarget(this, callfunc_selector(ExtendActionLayer::onCallBack1))); void ExtendActionLayer::onCallBack1() { flight->runAction(CCTintBy::actionWithDuration(0.5f, 255, 0, 255)); } 2、CCCallFuncO,回调,O表示CCObject,CCCallFunco可以带CCObject参数的函数 [cpp] CCSequence::actions(CCCallFuncO::actionWithTarget(this, callfuncO_selector(ExtendActionLayer::onCallBack2), CCInteger::create(2)); void ExtendActionLayer::onCallBack2(CCObject* data) { CCInteger* num = dynamic_cast<CCInteger*>(data); pSender->runAction(CCTintBy::actionWithDuration(num->getValue(), 255, 0, 255)); } 3、CCCallFuncN,回调,N表示CCNode,表示可以传递Node*类型 [cpp] CCSequence::actions(CCCallFuncN::actionWithTarget(this, callfuncN_selector(ExtendActionLayer::onCallBack3))); void ExtendActionLayer::onCallBack3(CCNode* pSender) { // 在这个例子里,pSender就是flight,因为是他执行了那个actionF pSender->runAction(CCTintBy::actionWithDuration(1.0f, 255, 0, 255)); } 4、CCCallFuncND,回调,ND表示CCNode和Data,可以传递对象本身和一个void*类型参数,void*表示可以是任意类型的参数 [cpp] CCSequence::actions(CCCallFuncND::actionWithTarget(this, callfuncND_selector(ExtendActionLayer::onCallBack4), (void *)2)); void ExtendActionLayer::onCallBack4(CCNode* pSender, void* pData) { pSender->runAction(CCTintBy::actionWithDuration(static_cast<float>((int)pData), 255, 0, 255)); }