首页 技术 正文
技术 2022年11月7日
0 收藏 360 点赞 801 浏览 3050 个字

转自: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(, ccp(,)),
CCCallFunc::create(this, callfunc_selector(testCallFunc::callback1)),
NULL); //CCCallFuncN的使用
CCFiniteTimeAction* action2 = CCSequence::create(
CCScaleBy::create( , ),
CCFadeOut::create(),
CCCallFuncN::create(this, callfuncN_selector(testCallFunc::callback2)),
NULL); //CCCallFuncNC的使用
CCFiniteTimeAction* action3 = CCSequence::create(
CCRotateBy::create( , ),
CCFadeOut::create(),
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", );
label->setPosition(ccp( s.width/*,s.height/)); addChild(label);
}void testCallFunc::callback2(CCNode* pSender)
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::create("callback 2 called", "Marker Felt", );
label->setPosition(ccp( s.width/*,s.height/)); addChild(label);
}void testCallFunc::callback3(CCNode* pTarget, void* data)
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::create("callback 3 called", "Marker Felt", );
label->setPosition(ccp( s.width/*,s.height/));
addChild(label);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901