cocos2d-x中CCCallFunc CCCallFuncN CCCallFuncND的区别和使用示例

CCCallFunc CCCallFuncN CCCallFuncND的区别和使用

CCCallFunc CCCallFuncN CCCallFuncND都用来创建带有回调函数的动作,区别主要在于回调函数是否带有参数

CCCallFunc

CCCallFunc是执行对应的回调函数,其中回调函数不可带参数。一般使用静态成员函数create创建实例,create声明如下:

staticCCCallFunc* create	(CCObject* 	pSelectorTarget,

SEL_CallFunc 	selector

)

回调函数通过execute方法执行,CCCallFunc中的execute的实现如下:

voidCCCallFunc::execute(){if(m_pCallFunc){(m_pSelectorTarget->*m_pCallFunc)();}if(m_nScriptHandler){CCScriptEngineManager::sharedManager()->getScriptEngine()->executeCallFuncActionEvent(this);}}

通过(m_pSelectorTarget->*m_pCallFunc)();可以看到回调函数不包含参数

CCCallFuncN

CCCallFuncN也是执行对应的回调函数,其中回调函数带一个参数。一般使用静态成员函数create创建实例,create声明如下:

staticCCCallFuncN* create	(CCObject* 	pSelectorTarget,

SEL_CallFuncN 	selector

)

回调函数通过execute方法执行,CCCallFuncN中的execute的实现如下:

voidCCCallFuncN::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

CCCallFuncND也是执行对应的回调函数,其中回调函数可带两个参数。一般使用静态成员函数create创建实例,create声明如下:

staticCCCallFuncND* create	(CCObject* 	pSelectorTarget,

SEL_CallFuncND 	selector,void* 	d

)

回调函数通过execute方法执行,CCCallFuncND中的execute的实现如下:

voidCCCallFuncND::execute(){if(m_pCallFuncND){(m_pSelectorTarget->*m_pCallFuncND)(m_pTarget, m_pData);}}

通过(m_pSelectorTarget->*m_pCallFuncND)(m_pTarget, m_pData);可以看到回调函数包含两个参数。

CCCallFunc CCCallFuncN CCCallFuncND实例对比

testCallFunc.h中代码class testCallFunc :publicCCLayer{protected:CCSprite*    sprite1;CCSprite*    sprite2;CCSprite*    sprite3;public:virtualvoid 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);}
本文链接: http://xiandanboke.com.cn/?p=957转载请注明转自 咸蛋博客

你可能感兴趣的:(cocos2d-x)