Cocos2d-x 3.x basic learning: callback function std::bind summary

Since Cocos2d-x 3.0 cited the C++11 standard, the callback function has adopted new function adapters: std::function, std::bind. The former callback functions menu_selector, callfunc_selector, cccontrol_selector, etc. have been ruthlessly abandoned. Instead, there is a series of CC_CALLBACK_*.

【std::bind】

1、CC_CALLBACK_*

Cocos2d-x uses a total of 4 std::bind macro definitions. The key point is to use std::bind for function adaptation.

std::placeholders::_1: Variable parameters. Not specified in advance, but passed in when calling.

##__VA_ARGS__: Variable parameter list.

//

// 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__)

//

2. Changed callback function

Action function: CallFunc/CallFuncN/callfunc_selector/callfuncN_selector/callfuncND_selector

Menu item callback: menu_selector

Touch event: onTouchBegan / onTouchMoved / onTouchEnded

2.1, action function CallFunc

You can directly use CC_CALLBACK_0, CC_CALLBACK_1, or you can directly use std::bind.

CallFunc: Use CC_CALLBACK_0. Without any uncertain parameters.

CallFuncN: Use CC_CALLBACK_1. The indefinite parameter placeholders::_1 needs to be passed in by default, and its value is: the object that calls the action (such as sprite->runAction(callfun), then the default indefinite parameter_1 is sprite).

//

/**

* Function action

* - CallFunc

* - CallFuncN

*-CallFuncND and CallFuncO have been abandoned, please use CallFuncN instead

*/

//2.x version

CallFunc::create (this, callfunc_selector (HelloWorld::callback0) );

CCCallFuncN::create (this, callfuncN_selector (HelloWorld::callback1) );

CCCallFuncND::create(this, callfuncND_selector(HelloWorld::callback2), (void *)10 );

//Callback

void HelloWorld::callback0() {} //CCCallFunc callback function

void HelloWorld::callback1(CCNode* node) {} //CCCallFuncN callback function

void HelloWorld::callback2(CCNode* node,void* a) {} //CCCallFuncND callback function, the parameter must be void*

//3.x version

//Use 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));

//Use std::bind

//The sprite is the sprite that performs the action

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));

//Callback

void HelloWorld::callback0() { }

void HelloWorld::callback1(Node* node) { }

void HelloWorld::callback2(Node* node, float a) {} //Customizable parameter type float

//

Of course, if you are familiar with std::bind, you can also use std::bind for the binding of CallFunc and CallFuncN callback functions.

As follows:

//

//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));

//Callback

void HelloWorld::callback0() { }

void HelloWorld::callback1(Node* node) { }

void HelloWorld::callback2(Node* node, float a) {} //Customizable parameter type float

//

2.2, menu item callback menu_selector

Use CC_CALLBACK_1, or use std::bind directly.

//

//2.x version

MenuItemImage::create("1.png", "2.png", this, menu_selector(HelloWorld::callback));

//3.x version

//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));

//Callback

void HelloWorld::callback(Node* sender) { }

//

2.3, touch event callback

Use CC_CALLBACK_2.

//

//Create an event listener type for single touch

auto touchLisner = EventListenerTouchOneByOne::create();

//Binding event

touchLisner->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);

touchLisner->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);

touchLisner->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);

//Callback

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);

//

3. The unchanged callback function

3.1, timer callback schedule_selector

The schedule_selector is still used.

//

//Timer

schedule(schedule_selector(HelloWorld::update), 1.0/60.0);

//Callback

void HelloWorld::update(float dt) { }

//

3.2. Button event callback cccontrol_selector

Still use cccontrol_selector.

//

//Button event binding

button->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::callback), Control::EventType::TOUCH_DOWN);

//Callback

void HelloWorld::callback(Node* sender, Control::EventType controlEvent) { }

//

4. Extended callback function

In the 3.x version, the event callback function can take any number of custom parameters.

For example: (take the menu item callback function as an example)

Please see the callback function callback4.

//

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);

//Callback

void HelloWorld::callback4(Node* sender, Sprite* bg, int a, float b)

{

bg->setScale(a * b);

}

//

Guess you like

Origin blog.csdn.net/qq_21743659/article/details/108616211