关于CCCallFunc、CCCallFuncN和CCCallFuncND的使用

cocos2d(x)中有3种action回调方式:CCCallFunc、CCCallFuncN和CCCallFuncND

 

看源代码的excute方法,我总结下面3点:

1. CCCallFunc

 CCCallFunc是执行对应的回调方法,其中回调方法不可带参数。

 

2. CCCallFuncN

  CCCallFuncN多带一个参数,而参数本身是动作Aciton本身。

 

3. CCCallFuncND

CCCallFuncND带2个参数,一个是动作Action本身,一个是你自己定义的回调函数的参数。


请看tests代码:

创建动作:

CCCallFunc::actionWithTarget(this, callfunc_selector(ActionSequence2::callback1)),
CCCallFuncN::actionWithTarget(this, callfuncN_selector(ActionSequence2::callback2)),
CCCallFuncND::actionWithTarget(this, callfuncND_selector(ActionSequence2::callback3), (void*)0xbebabeba),

回调函数:

void ActionSequence2::callback1()
{
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    CCLabelTTF *label = CCLabelTTF::labelWithString("callback 1 called", "Marker Felt", 16);
    label->setPosition(CCPointMake( s.width/4*1,s.height/2));

    addChild(label);
}

void ActionSequence2::callback2(CCNode* sender)
{
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    CCLabelTTF *label = CCLabelTTF::labelWithString("callback 2 called", "Marker Felt", 16);
    label->setPosition(CCPointMake( s.width/4*2,s.height/2));

    addChild(label);
}

void ActionSequence2::callback3(CCNode* sender, void* data)
{
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    CCLabelTTF *label = CCLabelTTF::labelWithString("callback 3 called", "Marker Felt", 16);
    label->setPosition(CCPointMake( s.width/4*3,s.height/2));

    addChild(label);
}





你可能感兴趣的:(action,callback)