转载请标明:转载自【小枫栏目】,博文链接http://blog.csdn.net/my183100521/article/details/10231311
一、简介
cocos2d-x常用回调函数有:CCCallFunc,CCCallFuncN,CCCallFuncND
它们之间区别:
CCCallFunc:执行对应的回调函数,其中回调函数不可带参数
CCCallFuncN:执行对应的回调函数,其中回调函数带一个参数,其中参数CCNode* sender,被传递到run这个action的对象中。
CCCallFuncND:和CCCallFuncN类似,不同的是(void*)agr0,这个是传给回调函数的参数。
二、实现代码
HelloWorldScene.cpp
CCSprite *RoleMan = CCSprite::create("Role.png");
RoleMan->setPosition(ccp(60, size.height/2));
this->addChild(RoleMan);
RoleMan->setVisible(false);
CCFiniteTimeAction* action = CCSequence::create(
CCPlace::create(ccp(200,200)),
CCShow::create(),
CCMoveBy::create(1, ccp(100,0)),
CCCallFunc::create(this,callfunc_selector(HelloWorld::callback1)),
CCCallFuncN::create(this,callfuncN_selector(HelloWorld::callback2)),
CCCallFuncND::create(this,callfuncND_selector(HelloWorld::callback3),(void*)0xbebabeba),
NULL);
RoleMan->runAction(action);
回调方法
void HelloWorld::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 HelloWorld::callback2(CCNode* sender)
{
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 HelloWorld::callback3(CCNode* sender, 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://blog.csdn.net/yanghuiliu/article/details/6933277