转自http://shahdza.blog.51cto.com/2410787/1553051
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@
|
34
35
36
* -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@
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@
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@
18
//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@
HelloWorld::callback0(){}@H_502_110@
HelloWorld::callback1(Node*node){}@H_502_110@
2.2、菜单项回调menu_selector
15
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、触控事件回调
14
//绑定事件@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@
virtual@H_502_110@
bool@H_502_110@
HelloWorld::onTouchBegan(Touch*touch,Event*unused_event);@H_502_110@
HelloWorld::onTouchEnded(Touch*touch,Event*unused_event);@H_502_110@
3、未变更的回调函数
7
schedule(schedule_selector(HelloWorld::update),1.0/60.0);@H_502_110@
3.2、按钮事件回调cccontrol_selector
7
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、扩展回调函数
22
"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@
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@