Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dx[3.2](11)——新回调函数std::bind大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

【唠叨】

自从3.0引用了C++11标准后,回调函数采用的新的函数适配器:std::functionstd::bind

而曾经的回调函数menu_SELEctor、callfunc_SELEctor、cccontrol_SELEctor等都已经被无情的抛弃了。

取而代之的则是一系列的CC_CALLBACK_*


【致谢】

http://www.voidcn.com/article/p-gjmsybrk-yt.html

http://www.voidcn.com/article/p-zsmxtvvc-bcy.html



【std::bind】

0、std::bind

请参照上面两篇文章。

1、CC_CALLBACK_*

cocos2dx总共使用了4个std::bind的宏定义,其重点就在于使用了std::bind进行函数适配

>std::placeholders::_1:不定参数。不事先指定,而是在调用的时候传入。

##__VA_ARGS__ :可变参数列表。

1
2
3
4
5
6
7
//
//newcallBACksbasedonC++11
#defineCC_CALLBACK_0(__SELEctor__,__target__,...)std::bind(&__SELEctor__,##__VA_ARGS__)
#defineCC_CALLBACK_1(__SELEctor__,std::placeholders::_1,##__VA_ARGS__)
#defineCC_CALLBACK_2(__SELEctor__,std::placeholders::_2,##__VA_ARGS__)
#defineCC_CALLBACK_3(__SELEctor__,std::placeholders::_3,##__VA_ARGS__)
//

2、变更的回调函数

动作函数 :CallFunc/CallFuncN

callfunc_SELEctor/callfuncN_SELEctor/callfuncND_SELEctor

菜单项回调@H_553_1@menu_SELEctor

触摸事件 onTouchBegan/onTouchMoved/onTouchEnded

2.1、动作函数CallFunc

可以直接使用CC_CALLBACK_0、CC_CALLBACK_1,也可以直接使用std::bind。

>CallFunc:使用CC_CALLBACK_0。不带任何不定参数。

CallFuncN:使用CC_CALLBACK_1。需要默认传入不定参数placeholders::_1,其值为:调用该动作的对象(如sprite->runAction(callfun),那么默认的一个不定参数 _1 为 sprite)。

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@H_345_262@ 28
29
30
31
32
33
34
35
36
/**
* 函数动作
* -CallFunc
* -CallFuncN
* -CallFuncND与CallFuncO已被遗弃,请使用CallFuncN替代
*/
//2.x版本
CallFunc::create( this ,callfunc_SELEctor(HelloWorld::callBACk0));
CCCallFuncN::create( :1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,callfuncN_SELEctor(HelloWorld::callBACk1));
CCCallFuncND::create( :1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,callfuncND_SELEctor(HelloWorld::callBACk2),( void *)10);
//回调函数
HelloWorld::callBACk0(){} //CCCallFunc回调函数
HelloWorld::callBACk1(CCNode*nodE){} //CCCallFuncN回调函数
HelloWorld::callBACk2(CCNode*node, *a){} //CCCallFuncND回调函数,参数必须为void*
//3.x版本
//使用CC_CALLBACK_*
CallFunc::create(CC_CALLBACK_0(HelloWorld::callBACk0,monospace!important; font-size:1em!important; min-height:inherit!important">));
CallFuncN::create(CC_CALLBACK_1(HelloWorld::callBACk1,monospace!important; font-size:1em!important; min-height:inherit!important">));
CallFuncN::create(CC_CALLBACK_1(HelloWorld::callBACk2,0.5));
//使用std::bind
//其中sprite为执行动作的精灵
CallFunc::create(std::bind(&HelloWorld::callBACk0,monospace!important; font-size:1em!important; min-height:inherit!important">));
CallFuncN::create(std::bind(&HelloWorld::callBACk1,spritE);
CallFuncN::create(std::bind(&HelloWorld::callBACk2,sprite,0.5));
//回调函数
HelloWorld::callBACk0(){}
HelloWorld::callBACk1(Node*nodE){}
HelloWorld::callBACk2(Node*node, float a){} //可自定义参数类型float
@H_489_442@

当然,如果你对于std::bind很熟悉的话,对于CallFunc、CallFuncN回调函数的绑定,也可以全部都使用std::bind。

如下所示:

18
//
//callBACk0
));
//callBACk1
CallFunc::create(std::bind(&HelloWorld::callBACk1,spritE));
:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,std::placeholders::_1));
//callBACk2
CallFunc::create(std::bind(&HelloWorld::callBACk2,0.5));
:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,0.5));
//回调函数
HelloWorld::callBACk0(){}
HelloWorld::callBACk1(Node*nodE){}
//可自定义参数类型float
2.2、菜单项回调menu_SELEctor

使用CC_CALLBACK_1,也可以直接使用std::bind。

15
//2.x版本
@H_533_285@menuItemImage::create( "1.png" :1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, "2.png" :1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,menu_SELEctor(HelloWorld::callBACk));
//3.x版本
//CC_CALLBACK_1
));
//std::bind
:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,std::bind(&HelloWorld::callBACk1,std::placeholders::_1));
HelloWorld::callBACk(Node*sender){}
2.3、触控事件回调

使用CC_CALLBACK_2

14
//创建一个事件监听器类型为单点触摸
auto touchLisner=EventListenerTouchOneByOne::create();
//绑定事件
touchLisner->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,monospace!important; font-size:1em!important; min-height:inherit!important">);
touchLisner->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,monospace!important; font-size:1em!important; min-height:inherit!important">);
touchLisner->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,monospace!important; font-size:1em!important; min-height:inherit!important">);
//回调函数
virtual bool HelloWorld::onTouchBegan(Touch*touch,Event*unused_event);
virtual HelloWorld::onTouchMoved(Touch*touch,Event*unused_event);
HelloWorld::onTouchEnded(Touch*touch,Event*unused_event);
3、未变更的回调函数

3.1、定时器回调schedule_SELEctor

依旧使用schedule_SELEctor

7
//定时器
schedule(schedule_SELEctor(HelloWorld::updatE),1.0/60.0);
//回调函数
HelloWorld::update( dt){}
3.2、按钮事件回调cccontrol_SELEctor

cccontrol_SELEctor

7
//按钮事件绑定
button->addTargetWithActionForControlEvents( :1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,cccontrol_SELEctor(HelloWorld::callBACk),Control::EventType::TOUCH_DOWN);
HelloWorld::callBACk(Node*sender,Control::EventTypecontrolEvent){}
4、扩展回调函数

在3.x版本中,事件的回调函数可以带任意个自定义的参数啦。

举个栗子:(以菜单项回调函数为例)

请看回调函数callBACk4。

22
sprite=Sprite::create(
"CloseNormal.png" );
sprite->setPosition(Vec2(visibleSize/2));
->addChild(spritE);
itemImage=MenuItemImage::create(
String" style="padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
std::bind(&HelloWorld::callBACk4,10,monospace!important; font-size:1em!important; min-height:inherit!important">itemImage->setPosition(Vec2(visibleSize/4));
pMenu=Menu::create(itemImage,null);
pMenu->setPosition(Vec2::ZERO);
->addChild(pMenu);
//回调函数
HelloWorld::callBACk4(Node*sender,Sprite*bg,monospace!important; font-weight:bold!important; font-size:1em!important; min-height:inherit!important; color:gray!important">int a,monospace!important; font-size:1em!important; min-height:inherit!important">b)
{
bg->setScale(a*b);
}
本文出自 “夏天的风” 博客,请务必保留此出处http://www.voidcn.com/article/p-kiicpnup-wx.html

大佬总结

以上是大佬教程为你收集整理的cocos2dx[3.2](11)——新回调函数std::bind全部内容,希望文章能够帮你解决cocos2dx[3.2](11)——新回调函数std::bind所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。