微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2dx[3.2](11)——新回调函数std::bind

转自http://shahdza.blog.51cto.com/2410787/1553051


【唠叨】

自从3.0引用了C++11标准后,回调函数采用的新的函数适配器:std::functionstd::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
//@H_502_110@
//newcallbacksbasedonC++11@H_502_110@
#defineCC_CALLBACK_0(__selector__,__target__,...)std::bind(&__selector__,##__VA_ARGS__)@H_502_110@
#defineCC_CALLBACK_1(__selector__,std::placeholders::_1,##__VA_ARGS__)@H_502_110@
#defineCC_CALLBACK_2(__selector__,std::placeholders::_2,##__VA_ARGS__)@H_502_110@
#defineCC_CALLBACK_3(__selector__,std::placeholders::_3,##__VA_ARGS__)@H_502_110@
//@H_502_110@

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
@H_502_277@ 33
34
35
36
/**@H_502_110@
@H_502_110@ * 函数动作@H_502_110@
* -CallFunc@H_502_110@
* -CallFuncN@H_502_110@
* -CallFuncND与CallFuncO已被遗弃,请使用CallFuncN替代@H_502_110@
*/@H_502_110@
//2.x版本@H_502_110@
@H_502_110@ CallFunc::create(@H_502_110@ this@H_502_110@ ,callfunc_selector(HelloWorld::callback0));@H_502_110@
CCCallFuncN::create(@H_502_110@ ottom: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,callfuncN_selector(HelloWorld::callback1));@H_502_110@
CCCallFuncND::create(@H_502_110@ ottom: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,callfuncND_selector(HelloWorld::callback2),(@H_502_110@ void@H_502_110@ *)10);@H_502_110@
@H_502_110@
//回调函数@H_502_110@
@H_502_110@ HelloWorld::callback0(){}@H_502_110@ //CCCallFunc回调函数@H_502_110@
HelloWorld::callback1(CCNode*node){}@H_502_110@ //CCCallFuncN回调函数@H_502_110@
HelloWorld::callback2(CCNode*node,@H_502_110@ *a){}@H_502_110@ //CCCallFuncND回调函数,参数必须为void*@H_502_110@
//3.x版本@H_502_110@
//使用CC_CALLBACK_*@H_502_110@
CallFunc::create(CC_CALLBACK_0(HelloWorld::callback0,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">));@H_502_110@
CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback1,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">));@H_502_110@
CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback2,0.5));@H_502_110@
@H_502_110@
//使用std::bind@H_502_110@
//其中sprite为执行动作的精灵@H_502_110@
CallFunc::create(std::bind(&HelloWorld::callback0,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">));@H_502_110@
CallFuncN::create(std::bind(&HelloWorld::callback1,sprite);@H_502_110@
CallFuncN::create(std::bind(&HelloWorld::callback2,sprite,0.5));@H_502_110@
@H_502_110@
//回调函数@H_502_110@
HelloWorld::callback0(){}@H_502_110@
HelloWorld::callback1(Node*node){}@H_502_110@
HelloWorld::callback2(Node*node,@H_502_110@ float@H_502_110@ a){}@H_502_110@ //可自定义参数类型float@H_502_110@
@H_502_110@

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

如下所示:

18
//@H_502_110@
//callback0@H_502_110@
));@H_502_110@
@H_502_110@
//callback1@H_502_110@
CallFunc::create(std::bind(&HelloWorld::callback1,sprite));@H_502_110@
ottom: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,std::placeholders::_1));@H_502_110@
//callback2@H_502_110@
CallFunc::create(std::bind(&HelloWorld::callback2,0.5));@H_502_110@
ottom: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,0.5));@H_502_110@
@H_502_110@
//回调函数@H_502_110@
HelloWorld::callback0(){}@H_502_110@
HelloWorld::callback1(Node*node){}@H_502_110@
//可自定义参数类型float@H_502_110@
2.2、菜单项回调menu_selector

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

15
//2.x版本@H_502_110@
MenuItemImage::create(@H_502_110@ "1.png"@H_502_110@ ottom: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,@H_502_110@ "2.png"@H_502_110@ ottom: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,menu_selector(HelloWorld::callback));@H_502_110@
@H_502_110@
//3.x版本@H_502_110@
//CC_CALLBACK_1@H_502_110@
));@H_502_110@
//std::bind@H_502_110@
ottom: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,std::bind(&HelloWorld::callback1,std::placeholders::_1));@H_502_110@
@H_502_110@
HelloWorld::callback(Node*sender){}@H_502_110@
2.3、触控事件回调

使用CC_CALLBACK_2

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

3.1、定时器回调schedule_selector

依旧使用schedule_selector

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

cccontrol_selector

7
//按钮事件绑定@H_502_110@
button->addTargetWithActionForControlEvents(@H_502_110@ ottom: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,cccontrol_selector(HelloWorld::callback),Control::EventType::TOUCH_DOWN);@H_502_110@
HelloWorld::callback(Node*sender,Control::EventTypecontrolEvent){}@H_502_110@
4、扩展回调函数

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

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

请看回调函数callback4。

22
sprite=Sprite::create(@H_502_110@ "Closenormal.png"@H_502_110@ );@H_502_110@
sprite->setPosition(Vec2(visibleSize/2));@H_502_110@
->addChild(sprite);@H_502_110@
itemImage=MenuItemImage::create(@H_502_110@
@H_502_110@ ottom: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,@H_502_110@
ottom: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,@H_502_110@
std::bind(&HelloWorld::callback4,10,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">itemImage->setPosition(Vec2(visibleSize/4));@H_502_110@
pMenu=Menu::create(itemImage,NULL);@H_502_110@
pMenu->setPosition(Vec2::ZERO);@H_502_110@
->addChild(pMenu);@H_502_110@
@H_502_110@
@H_502_110@
//回调函数@H_502_110@
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@H_502_110@ a,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">b)@H_502_110@
{@H_502_110@
bg->setScale(a*b);@H_502_110@
}@H_502_110@
//@H_502_110@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐