cocos2dx[3.2](11)——新回调函数std::bind
发布时间:2020-12-14 21:39:47 所属栏目:百科 来源:网络整理
导读:【唠叨】 自从3.0引用了 C++11标准 后,回调函数采用的新的函数适配器: std::function 、 std::bind 。 而曾经的回调函数menu_selector、callfunc_selector、cccontrol_selector等都已经被无情的抛弃了。 取而代之的则是一系列的 CC_CALLBACK_* 。 【致谢】
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(
//回调函数
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
|
//
//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
|
//2.x版本
MenuItemImage::create(
"1.png"
"2.png"
//3.x版本
//CC_CALLBACK_1
));
//std::bind
HelloWorld::callback(Node*sender){}
2.3、触控事件回调
|
//创建一个事件监听器类型为单点触摸
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、未变更的回调函数
|
//定时器
schedule(schedule_selector(HelloWorld::update),1.0/60.0);
//回调函数
HelloWorld::update(
dt){}
3.2、按钮事件回调cccontrol_selector
|
//按钮事件绑定
button->addTargetWithActionForControlEvents(
4、扩展回调函数
|
sprite=Sprite::create(
"CloseNormal.png"
);
sprite->setPosition(Vec2(visibleSize/2));
->addChild(sprite);
itemImage=MenuItemImage::create(
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.52php.cn/article/p-kiicpnup-wx.html |