Cocos2dx 3.0 transition article (17) bind and CC_CALLBACK

Source: Internet
Author: User

The topic of this article is to reveal the secrets between CC_CALLBACK and std: bind ......

First, let's look at a piece of code:

// Create three genie boys = Sprite: create ("boy.png"); // create boyboy-> setPosition (Point (visibleSize. width/2, visibleSize. height/2); this-> addChild (boy, 1); girl_1 = Sprite: create ("girl_1.png"); // create girl1girl_1-> setPosition (Point (visibleSize. width/3, visibleSize. height/2); girl_1-> setTag (10); this-> addChild (girl_1, 1); girl_2 = Sprite: create ("girl_3.png "); // create girl2girl_2-> setPosition (Point (2 * visibleSize. width/3, visibleSize. height/2); girl_2-> setTag (20); this-> addChild (girl_2, 1); // Let the boy exercise, call back callback1boy-> runAction (CCSequence: create (MoveBy: create (1.0f, Point (0,100), Callfunc: create (CC_CALLBACK_0 (HelloWorld: callback1, this), NULL ));
Implementation of the three callback functions:

Void HelloWorld: callback1 () {CCLOG ("in callback1"); // call back the girl1 motion to callback2girl_1-> runAction (CCSequence: create (MoveBy :: create (1.0f, Point (0,150), CallFunc: create (CC_CALLBACK_0 (HelloWorld: callback2, this, girl_1), NULL);} void HelloWorld :: callback2 (Node * sender) {// girl2 motion, callback to callback3girl_2-> runAction (CCSequence: create (MoveBy: create (1.0f, Point (0,200), CallFunc:: create (CC_CALLBACK_0 (HelloWorld: c Allback3, this, girl_2, 99), NULL); CCLOG ("in callback2, sender tag is: % d", (Sprite *) sender-> getTag ();} void HelloWorld: callback3 (Node * sender, long data) {// final output CCLOG ("in callback3, everything is OK, sender tag is: % d, date is: % ld ", (Sprite *) sender-> getTag (), data); CCLOG (" girl2 dandan ask: what fake the CC_CALLBACK is? ");}

The whole process is boy "seduce" girl1, but girl1 is obviously not very interested in the opposite sex, so she also seduce girl2 ...... however, girl2 is not interested in the same-sex. She just said: What is CC_CALLBACK ?,Debugging


Okay, let me get back to the mouth and then answer girl2's question: what is the cake (Dongdong) in CC_CALLBACK )?
Let's take a look at the advanced CC_CALLBACK source code:

// 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__)
After reading it, I suddenly realized that! Don't read, don't know. Just look at it...
Here, we mainly pay attention to two points: std: bind, and ##_ VA_ARGS _; ##_ VA_ARGS _ is a variable parameter macro.
. Focus on std: bind.
Std: bind is a new Member in C ++ 11. You can regard the bind function as a universal function adapter. It accepts a callable object and generates a new callable object to "Adapt" to the parameter list of the original object.
The general form of calling bind is:
Auto newCallback = bind (callback, arg_list );
Among them, newCallback is a callable object, and arg_list is a list of parameters that can be separated by commas (,). As for what parameters, you can see what parameters are in the callback function. That is to say, when newCallback is called, newCallback calls the callback function and passes the arg_list parameter to callback.

After reading the above content, your understanding may be vague. For example, there is a function callback, as shown below,

int callback(int one,char two,double three);
Next we use bind to call callback.

Auto newCallback = bind (callback, _ 1, _ 2, 1.5); int x = newCallback (10, 'H'); // This sentence is equivalent: int x = callback (10, 'h', 1.5 );
"_ 1" is a placeholder object used to indicate the position of the first parameter of the function newCallback In the parameter list of the function callback when the function callback is called through the function newCallback. The first parameter is called "_ 1", and the second parameter is "_ 2". It is interesting to push it like this. '1. 5' indicates the default parameter, which is behind _ 1 and _ 2, so it is a double parameter.
It is emphasized that placeholders such as _ 1 are defined in a namespace named placeholders, And the namespace itself is defined in the std namespace. To use these names, both namespaces must be written,
For example:

Std: placeholders: _ 1;

It is difficult to write a thief in this way. Therefore, when _ 1 is used, you can add the following sentence:
Using namespace namespace_name;Well, OK

Well, bind will introduce it here. It's a little bit simple. If you don't understand it, you can study it on Baidu. Finally, let's look at the definition of CC_CALLBACK. Is it much clearer?
Finally, let's give an example. It's still the old boy, girl1, and girl2, but they have to replace the way they send "love. Use std: bind instead of CC_CALLBACK. The Code is as follows:

// Call callback1boy-> runAction (CCSequence: create (MoveBy: create (1.0f, Point (0,100), Callfunc: create (std:: bind (& HelloWorld: callback1, this), NULL ));
Void HelloWorld: callback1 () {CCLOG ("in callback1"); // call back the girl1 motion to callback2girl_1-> runAction (CCSequence: create (MoveBy :: create (1.0f, Point (0,150), CallFunc: create (std: bind (& HelloWorld: callback2, this, girl_1), NULL )); CCLOG ("boy ask girl_1: can you do my girlFriends? ");} Void HelloWorld: callback2 (Node * sender) {// girl2 motion, callback to callback3girl_2-> runAction (CCSequence: create (MoveBy: create (1.0f, point (0,200), CallFunc: create (std: bind (& HelloWorld: callback3, this, girl_1, 99), NULL); CCLOG ("in callback2, sender tag is: % d ", (Sprite *) sender-> getTag (); CCLOG (" girl_1 ask girl_2: I love girl_2 ");} void HelloWorld :: callback3 (Node * sender, long data) {// final output CCLOG ("in callback3, ev Erything is OK, sender tag is: % d, date is: % ld ", (Sprite *) sender-> getTag (), data); CCLOG (" girl2 dandan say: I know how to use CC_CALLBACK! ");}


Well, that's it. The relationship between bind and CC_CALLBACK is so simple.


Respect Original, reproduced please destined from star te 530: http://blog.csdn.net/start530/article/details/21245565

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.