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

转自:http://xiandanboke.com.cn/cocos2d-xcccallfunc.html

CCCallFunc CCCallFuncN CCCallFuncND的区别和使用

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

CCCallFunc

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

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

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);可以看到回调函数包含两个参数。

CCCallFunc CCCallFuncN CCCallFuncND实例对比

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);
}

自己的范例:

                CCMoveTo* moveProp = CCMoveTo::create(0.35f, role->getSprite()->getPosition());

                CCArray *array = ActionFactory::getInstance()->getActionFrameByType(10);

                actionSprite = CCSprite::createWithSpriteFrame((CCSpriteFrame *)array->objectAtIndex(0));

                actionSprite->setPosition(prop->getPropSprite()->getPosition());

                CCLayer*layer = GameConfig::getInstance()->getCurrentLayer();

                layer->addChild(actionSprite, OTHER_Z);

                CCAnimation* animation = CCAnimation::createWithSpriteFrames(array, 0.08f);

                //水晶闪烁动画

                CCFiniteTimeAction* action1 = CCSequence::create(CCAnimate::create(animation), moveProp,

                                                                 CCCallFuncN::create(actionSprite, callfuncN_selector(PropManager::burstOver)),NULL);

                //跟随动画 无法跟随

                CCFollow* action2 = CCFollow::create(role->getSprite()); //, CCRectMake(0, 0, 10, 10)

                action2->setBoudarySet(true);

                //把两个动画串联起来

                CCSpawn* action = CCSpawn::create(action1, action2, NULL);

                actionSprite->runAction(action1);

 

    void burstOver(CCObject *pSender)

    {

        CCSprite *psprite = (CCSprite*)pSender;

        psprite->removeFromParent();

    }

 

 
posted @ 2014-11-07 17:08  六界剑仙  阅读(139)  评论(0)    收藏  举报