Cocos2d-x 3.1.1 Study Notes [17] about those functions, cocos2d-x3.1.1

Source: Internet
Author: User
Tags addchild

Cocos2d-x 3.1.1 Study Notes [17] about those functions, cocos2d-x3.1.1
For cocos2d-x is often used in the method, have to study, this study has really gained.


First, define an genie to implement a series of continuous actions.

In order for an action to call back our functions, we must first declare and implement them.

Void callBack (); void callBack_1 (Node * node); void callBack_2 (Node * node, const char * str); void Nice: callBack () {log ("Nice :: callBack () ");} void Nice: callBack_1 (Node * node) {log (" This tag is % d ", node-> getTag ();} void Nice:: callBack_2 (Node * node, const char * str) {log ("This tag is % d, and str is % s", node-> getTag (), str );} // then we start to create our great genie. Sp_area = Sprite: create ("newAlwaysShow.png"); // sp_area is a class member, Sprite * ap_area; sp_area-> setTag (1314); auto sp_another = Sprite :: create (); sp_another-> setTag (520); addChild (sp_another); addChild (sp_area); // tumble, lovely spirit! /* The function created by CallFunc must be a function created by CallFuncN without parameters. It can be one or more parameters */sp_area-> runAction (Sequence: create (MoveTo: create (2, vec2 (200,200), CallFunc: create (CC_CALLBACK_0 (Nice: callBack, this), CallFuncN: create (CC_CALLBACK_1 (Nice: callBack_2, this, "I have tow parameter"), NULL); // After writing this code, you will find that the content in the runAction is so dull (sometimes it is not true !), So I wrote a story about it and turned it into a cruel one. Sp_area-> runAction (Sequence: create (MoveTo: create (2, Vec2 (200,200), CallFunc: create (CC_CALLBACK_0 (Nice: callBack, this )), callFunc: create ([&] ()-> void {log ("Done1"); log ("This tag is % d ", sp_area-> getTag ();}), CallFunc: create (std: bind (& Nice: callBack, this), CallFuncN: create (CC_CALLBACK_1 (Nice:: callBack_1, this), // 1314 CallFuncN: create (CC_CALLBACK_0 (Nice: callBack_1, this, sp_another), // 520 CallFuncN :: create ([] (Node * node) {log ("this tag is % d", node-> getTag () ;}), // 1314 CallFuncN :: create (std: bind (& Nice: callBack_1, this, sp_area), // 1314 CallFuncN: create (std: bind (& Nice: callBack_1, this, sp_another), // 520 CallFuncN: create (CC_CALLBACK_1 (Nice: callBack_2, this, "I have tow parameter"), NULL ));


Well, he's totally out of sight now. In fact, so many rows only use two methods, the first lambda expression and the second std: bind (). (Nima, are you kidding me? Isn't there CC_CALLBACK_0 ?)
Amount. Now let's look at the 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__)



Sure enough, the macro is just a surface, and it is secretly hooked up with std: bind!


Let's take a look at how the macro is defined everywhere ~~~
# Define CC_CALLBACK_0 (_ selector __,__ target __,...) std: bind (&__ selector __,__ target __, ##__ VA_ARGS __)
# Define CC_CALLBACK_0 (method, target, variable parameter) std: bind (& obtain method address, target, variable parameter)
PS: it cannot be used when defining macros... as before... as a variable parameter, you must replace it with ##__ VA_ARGS _, a great third party.


Variable parameters are also used in the entire cocos-x. For example, creating Sequence: create is a motion. Otherwise, how can you input so many parameters when creating a series of actions.
Then let's take a look at the source code. (Source code again --)


Sequece: create is implemented by platform (win and others) # if (CC_TARGET_PLATFORM = CC_PLATFORM_WP8) | (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) // WP8 in VS2012 does not support nullptr in variable args lists and variadic templates are also not supported typedef FiniteTimeAction * M; static Sequence * create (M m1, std: nullptr_t listEnd) {return variadicCreate (m1, NULL);} static Sequence * create (M m1, M m2, std: nullptr_t listEnd) {return variadicCreate (m1, m2, NULL );} static Sequence * create (M m1, M m2, M m3, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, NULL );} static Sequence * create (M m1, M m2, M m3, M m4, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, m4, NULL );} static Sequence * create (M m1, M m2, M m3, M m4, M m5, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, m4, m5, NULL);} static Sequence * create (M m1, M m2, M m3, M m4, M m5, M m6, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, m4, m5, m6, NULL);} static Sequence * create (M m1, M m2, M m3, M m4, M m5, M m6, M m7, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, m4, m5, m6, m7, NULL);} static Sequence * create (M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, m4, m5, m6, m7, m8, NULL);} static Sequence * create (M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, m4, m5, m6, m7, m8, m9, NULL);} static Sequence * create (M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, M m10, std: nullptr_t listEnd) {return variadicCreate (m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, NULL);} // On WP8 for variable argument lists longer than 10 items, use the other create functions or variadicCreate with NULL as the last argument static Sequence * variadicCreate (FiniteTimeAction * item ,...); # else static Sequence * create (FiniteTimeAction * action1 ,...) CC_REQUIRES_NULL_TERMINATION; # endifSequence * Sequence: create (FiniteTimeAction * action1 ,...) {va_list params; va_start (params, action1); Sequence * ret = Sequence: createWithVariableList (action1, params); // it is passed to createWithVariableList for va_end (params ); return ret;} Sequence * Sequence: createWithVariableList (FiniteTimeAction * action1, va_list args) {FiniteTimeAction * now; FiniteTimeAction * prev = action1; bool bOneAction = true; while (action1) {now = va_arg (args, FiniteTimeAction *); // if the second action exists if (now) {prev = createWithTwoActions (prev, now); bOneAction = false ;} // If the second action does not exist else {// If only one action is added to Sequence, make up a Sequence by adding a simplest finite time action. if (bOneAction) {prev = createWithTwoActions (prev, ExtraAction: create () ;}break ;}return (Sequence *) prev );}

We will implement a Variable Parameter Function According to his idea.


Void manyArgument (int num ,...); void Nice: manyArgument (int num ,...) {va_list params; // The second parameter of the va_start function must be the same as that of the first parameter of manyArgument: va_start (params, num); int now; log ("fist is % d ", num); while (true) {now = va_arg (params, int); if (now! =-1) {log ("now is % d", now) ;}else {break ;}}va_end (params) ;}manyargument (1, 2, 3, 4, -1 );


When multiple parameters are found, there must be a condition that can be terminated (for example, the last one is-1). But what if-1 is required in the middle?
At this time, you can only change the input parameter to int *
Void manyArgument (int * num ,...);
Then, replace the final termination condition! = Null, but it is troublesome to pass in parameters (if any)


Void manyArgument (int * num ,...); void Nice: manyArgument (int * num ,...) {va_list params; // The second parameter of the va_start function must be the same as that of the first parameter of manyArgument: va_start (params, num); int * now; log ("fist is % d ", * num); while (true) {now = va_arg (params, int *); if (now! = Nullptr) {log ("now is % d", * now) ;}else {break ;}} va_end (params) ;}int arr [] {3, 6, 9, 12 }; manyArgument (arr, arr + 1, arr + 2, arr + 3, nullptr );



Next, let's try to define a variable parameter. The macro #__ VA_ARGS _ represents the variable parameter passed in.

    #define MANYARGUMENT(__num__, ...) manyArgument(__num__, ##__VA_ARGS__)    MANYARGUMENT(arr, arr+1, arr+2, nullptr);

Don't forget that we still have std: placeholders: _ 1, which has never been used? =


Auto add_num = std: bind (& Nice: add, this, std: placeholders: _ 1, std: placeholders: _ 2, 10 ); log ("sum is % d", add_num (1, 1); // 12 log ("sum is % d", add_num (1, 1, 2 )); // 12 log ("sum is % d", add_num (1, 1, 2, 4 )); // 12 // no matter how you change the parameter, the third digit is fixed to 10, and the magic is that four parameters can be passed in. Obviously, only the first two parameters are valid, all the following items are invalid.



Learn here. It seems that this process is quite clear. Although there may be more but not found, we still have more time to use!























How to customize a function in an existing class in a cocos2d-x?

Class CustomBaseSprite: public Sprite {public: void f ();};
 
C/c ++/is there a function of square and cocos2d-x?

Double pfh (double a, double B)
{
Return a * a + B * B;
}
Main ()
{
Double a, B;
Scanf ("% lf", & a, & B );
Printf ("% lf", pfh (a, B ));
}

Double

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.