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的繼承關係: 一、CCCallFunc,回調,調用不帶參數的函數: [cpp] CCSequence::actions(CCCallFunc::actionWithTarget(this, callfunc_selector(ExtendActionLayer::onCallBack1))); void ExtendActionLayer::onCallBack1() { flight->runAction(CCTintBy::actionWithDuration(0.5f, 255, 0, 255)); } 二、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)); } 三、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)); } 四、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)); }