——新回调函数std::bind,第1张 cocos2dx[3.2](11)——新回调函数std::bind,第1张](/aiimages/cocos2dx%5B3.2%5D%2811%29%E2%80%94%E2%80%94%E6%96%B0%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0std%3A%3Abind.png)
转自http://shahdza.blog.51cto.com/2410787/1553051
【唠叨】
自从3.0引用了C++11标准后,回调函数采用的新的函数适配器:std::function、std::bind。
而曾经的回调函数menu_selector、callfunc_selector、cccontrol_selector等都已经被无情的抛弃了。
取而代之的则是一系列的CC_CALLBACK_*。
【致谢】
http://www.jb51.cc/article/p-gjmsybrk-yt.html
http://www.jb51.cc/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
菜单项回调: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 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( CCCallFuncND::create( 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; background:none!important">)); CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback1,monospace!important; Font-size:1em!important; min-height:inherit!important; background:none!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; background:none!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 当然,如果你对于std::bind很熟悉的话,对于CallFunc、CallFuncN回调函数的绑定,也可以全部都使用std::bind。
如下所示:
18 //callback0 )); //callback1 CallFunc::create(std::bind(&HelloWorld::callback1,sprite)); //callback2 CallFunc::create(std::bind(&HelloWorld::callback2,0.5)); //回调函数 HelloWorld::callback0(){} HelloWorld::callback1(Node*node){} //可自定义参数类型float 2.2、菜单项回调menu_selector 使用CC_CALLBACK_1,也可以直接使用std::bind。
15 MenuItemImage::create( "1.png" "2.png" //3.x版本 //CC_CALLBACK_1 )); //std::bind 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; background:none!important">); touchlisner->ontouchmoved=CC_CALLBACK_2(HelloWorld::ontouchmoved,monospace!important; Font-size:1em!important; min-height:inherit!important; background:none!important">); touchlisner->ontouchended=CC_CALLBACK_2(HelloWorld::ontouchended,monospace!important; Font-size:1em!important; min-height:inherit!important; background:none!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( HelloWorld::callback(Node*sender,Control::EventTypecontrolEvent){} 4、扩展回调函数 在3.x版本中,事件的回调函数可以带任意个自定义的参数啦。
举个栗子:(以菜单项回调函数为例)
请看回调函数callback4。
22 "Closenormal.png" ); sprite->setposition(Vec2(visibleSize/2)); ->addChild(sprite); itemImage=MenuItemImage::create( std::bind(&HelloWorld::callback4,10,monospace!important; Font-size:1em!important; min-height:inherit!important; background:none!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; background:none!important">int a,monospace!important; Font-size:1em!important; min-height:inherit!important; background:none!important">b) { bg->setScale(a*b); } // 以上是内存溢出为你收集整理的cocos2dx[3.2](11)——新回调函数std::bind全部内容,希望文章能够帮你解决cocos2dx[3.2](11)——新回调函数std::bind所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)