cocos2dx[3.2](11)——新回调函数std::bind
发布时间:2020-12-14 16:27:19 所属栏目:百科 来源:网络整理
导读:原始出处 http://www.jb51.cc/article/p-kiicpnup-wx.html 【唠叨】 自从3.0引用了 C++11标准 后,回调函数采用的新的函数适配器: std::function 、 std::bind 。 而曾经的回调函数menu_selector、callfunc_selector、cccontrol_selector等都已经被无情的抛
原始出处
http://www.52php.cn/article/p-kiicpnup-wx.html
|
/**
* 函数动作
* -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:auto!important; background:none!important">));
CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback1,monospace!important; font-size:1em!important; min-height:auto!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:auto!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
//
|
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
//
15
MenuItemImage::create(
"1.png"
"2.png"
//3.x版本
//CC_CALLBACK_1
));
//std::bind
HelloWorld::callback(Node*sender){}
2.3、触控事件回调
14
auto
touchLisner=EventListenerTouchOneByOne::create();
//绑定事件
touchLisner->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">);
touchLisner->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">);
touchLisner->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,monospace!important; font-size:1em!important; min-height:auto!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、未变更的回调函数
7
schedule(schedule_selector(HelloWorld::update),1.0/60.0);
//回调函数
HelloWorld::update(
dt){}
3.2、按钮事件回调cccontrol_selector
7
button->addTargetWithActionForControlEvents(
HelloWorld::callback(Node*sender,Control::EventTypecontrolEvent){}
4、扩展回调函数
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:auto!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:auto!important; color:gray!important; background:none!important">int
a,monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">b)
{
bg->setScale(a*b);
}
//