http://shahdza.blog.51cto.com/2410787/1553051
1
2
3
4
5
6
7
| // // new callbacks based on C++11 #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__) #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__) #define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__) #define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__) // |
1
2
3
4
5
6
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 ( this , callfuncN_selector (HelloWorld::callback1) ); CCCallFuncND::create( this , callfuncND_selector(HelloWorld::callback2), ( void *)10 ); //回调函数 void HelloWorld::callback0() { } //CCCallFunc回调函数 void HelloWorld::callback1(CCNode* node) { } //CCCallFuncN回调函数 void HelloWorld::callback2(CCNode* node, void * a) { } //CCCallFuncND回调函数,参数必须为void* //3.x版本 //使用 CC_CALLBACK_* CallFunc::create ( CC_CALLBACK_0(HelloWorld::callback0, this ) ); CallFuncN::create( CC_CALLBACK_1(HelloWorld::callback1, this ) ); CallFuncN::create( CC_CALLBACK_1(HelloWorld::callback2, this , 0.5)); //使用 std::bind //其中sprite为执行动作的精灵 CallFunc::create (std::bind(&HelloWorld::callback0, this ) ); CallFuncN::create(std::bind(&HelloWorld::callback1, this , sprite); CallFuncN::create(std::bind(&HelloWorld::callback2, this , sprite, 0.5)); //回调函数 void HelloWorld::callback0() { } void HelloWorld::callback1(Node* node) { } void HelloWorld::callback2(Node* node, float a) { } //可自定义参数类型float // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| // //callback0 CallFunc::create(std::bind(&HelloWorld::callback0, this )); //callback1 CallFunc::create (std::bind(&HelloWorld::callback1, this , sprite)); CallFuncN::create(std::bind(&HelloWorld::callback1, this , std::placeholders::_1)); //callback2 CallFunc::create (std::bind(&HelloWorld::callback2, this , sprite, 0.5)); CallFuncN::create(std::bind(&HelloWorld::callback2, this , std::placeholders::_1, 0.5)); //回调函数 void HelloWorld::callback0() { } void HelloWorld::callback1(Node* node) { } void HelloWorld::callback2(Node* node, float a) { } //可自定义参数类型float // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| // //2.x版本 MenuItemImage::create( "1.png" , "2.png" , this , menu_selector(HelloWorld::callback)); //3.x版本 //CC_CALLBACK_1 MenuItemImage::create( "1.png" , "2.png" , CC_CALLBACK_1(HelloWorld::callback1, this )); //std::bind MenuItemImage::create( "1.png" , "2.png" , std::bind(&HelloWorld::callback1, this , std::placeholders::_1)); //回调函数 void HelloWorld::callback(Node* sender) { } // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| // //创建一个事件监听器类型为 单点触摸 auto touchLisner = EventListenerTouchOneByOne::create(); //绑定事件 touchLisner->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this ); touchLisner->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this ); touchLisner->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this ); //回调函数 virtual bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event); virtual void HelloWorld::onTouchMoved(Touch *touch, Event *unused_event); virtual void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event); // |
1
2
3
4
5
6
7
| // //定时器 schedule(schedule_selector(HelloWorld::update), 1.0/60.0); //回调函数 void HelloWorld::update( float dt) { } // |
1
2
3
4
5
6
7
| // //按钮事件绑定 button->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::callback), Control::EventType::TOUCH_DOWN); //回调函数 void HelloWorld::callback(Node* sender, Control::EventType controlEvent) { } // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| // auto sprite = Sprite::create( "CloseNormal.png" ); sprite->setPosition(Vec2(visibleSize / 2) ); this ->addChild(sprite); auto itemImage = MenuItemImage::create( "CloseNormal.png" , "CloseNormal.png" , std::bind(&HelloWorld::callback4, this , std::placeholders::_1, sprite, 10, 0.5)); itemImage->setPosition(Vec2(visibleSize / 4)); auto pMenu = Menu::create(itemImage, NULL); pMenu->setPosition(Vec2::ZERO); this ->addChild(pMenu); //回调函数 void HelloWorld::callback4(Node* sender, Sprite* bg, int a, float b) { bg->setScale(a * b); } // |