Cocos2d-x-3.0 alpha1 with C ++ 11 Exercise 3: dart ninja, how to move Genie and how to use lambda expressions

Source: Internet
Author: User

In the previous section, an genie is added to the game scenario. But a hero may be too lonely. We should add some enemies to defeat him. 1. Adding the void addTarget () function will help us complete this task. The enemy will move from the game scene left to the right at random speed. In HelloWorldScence. declare void addTarget () in h, and in HelloWorldScene. add the following code to cpp: void HelloWorld: addTarget () {Sprite * target = Sprite: create ("Target.png", Rect (0, 0, 27, 40 )); size winSize = Director: getInstance ()-> getWinSize (); int minY = target-> getContentSize (). height/2; int maxY = winSize. height-minY; int rangeY = maxY-minY; int actualY = rand () % rangeY + minY; target-> setPosition (Point (winSize. width + Target-> getContentSize (). width/2, actualY); this-> addChild (target); int actualDuration = rand () % 2 + 2; FiniteTimeAction * actionMove = MoveTo: create (actualDuration, point (0-target-> getContentSize (). width/2, actualY); // const std: function <void (Node *)> & func FiniteTimeAction * actionMoveDone = CallFuncN: create (std :: bind (& HelloWorld: spriteMoveFinished, this, target); target-> runAction (Sequence: Create (actionMove, actionMoveDone, NULL);} srand and rand are C standard library functions. Here is the cocos2d-x 3.0 writing, the most confusing is actionMoveDone initialization, due to the use of c ++ 11 new features, CallFuncN: create parameter is 1 const std :: function <void (Node *)> & func and std: bind can help us generate this parameter. CallFuncN is used to call back the spriteMoveFinished method. h. Declare and implement it as follows: void HelloWorld: spriteMoveFinished (Node * sender) {Sprite * sprite = (Sprite *) sender; this-> removeChild (sprite);} 2, when throwing out enemies at regular intervals, we should regularly Add the following code to the game before the return value of the init () function: this-> schedule (schedule_selector (HelloWorld: gameLogic ), 1.0); In HelloWorldScence. declare in h: void gameLogic (float dt); from 2. beginning with Version x, ccTime is replaced by float. Then in HelloWorldScence. implement gameLogic (): void HelloWorld: gameLogic (float dt) {this-> addTarget ();} in cpp: 3. Use lambda to override CallFuncN :: the create statement lambda is a new feature of c ++ 11. It is said that the performance is superior to the std: bind method. The lambda statement has the same effect: 1 // FiniteTimeAction * actionMoveDone = CallFuncN: create (std: bind (& HelloWorld: spriteMoveFinished, this, target )); 1 FiniteTimeAction * actionMoveDone = CallFuncN: create ([this] (Node * sender)-> void {// std: cout <"remove target" <std :: endl; this-> removeChild (sender) ;}); check CallFuncN: create Parameter definition: 1 const std: function <void (Node *)> the & func parameter must be a func. The return type is void, and the parameter list is Node *. The above lambda expressions exactly match. Appendix lambda expression Description: Lambda expressions in C ++ 11 are used to define and create anonymous function objects to simplify programming. Lambda Syntax: [function object parameters] (operator overload function parameters) mutable or exception Declaration-> return value type {function body}. Lambda consists of five parts: [function object parameters], (operator overload function parameters), mutable or exception declaration,-> return value type, {function body }. The following is an introduction. 1. [function object parameters] to identify the beginning of a Lambda. This part must exist and cannot be omitted. Function object parameters are passed to the constructor of the function object class automatically generated by the compiler. Function object parameters can only use local variables that are visible within the scope of Lambda when Lambda is defined (including this of the class where Lambda is located ). Function object parameters are in the following format: 1. null. No function object parameters are used. 2. =. The function body can use all visible local variables (including this of the Lambda class) within the scope of the Lambda function ), and it is the value transfer method (equivalent to the compiler automatically passing all local variables by value ). 3 ,&. The function body can use all visible local variables (including this of the Lambda class) within the scope of the Lambda function ), and it is the reference transfer method (equivalent to the compiler automatically passing all local variables for us by reference ). 4. this. The function body can use the member variables in the class where Lambda is located. 5.. Pass a by value. When passing by value, the function body cannot modify the copy of a passed in, because the function is const by default. To modify the copy of a passed in, you can add a mutable modifier. 6. &. Pass a by reference. 7. a, & B. Pass a by value, and B by reference. 8, =, & a, & B. Except a and B are passed by reference, other parameters are passed by value. 9. &, a, and B. Except a and B are passed by value, other parameters are passed by reference. II. (operator overload function parameters): identifies the parameters of the overloaded () operator. If no parameter exists, this part can be omitted. Parameters can be passed by value (for example, (a, B) or by reference (for example, (& a, & B. Iii. mutable or exception declaration. This part can be omitted. When passing function object parameters by value with the mutable modifier, you can modify the copy passed by value (note that the copy can be modified, rather than the value itself ). The exception declaration is used to specify the exceptions thrown by the function. For example, throw (int) can be used to throw an integer exception ). 4.-> return value type, which identifies the type of the function return value. When the return value is void or the function body contains only one return field (in this case, the compiler can automatically infer the return value type, this part can be omitted. 5. {function body}: indicates the implementation of the function. This part cannot be omitted, but the function body can be empty. 4, cocos2d-x from 1. x to 3. x's snail bait has only tried 1. version x, still impressed with CCArray: array (), CCScene: node () and other usage, but in 3. in Version x, these changes occur. It becomes simple and unified, but it is used and memorized. The original CC prefixes are basically removed. For example, CCScene is changed to Scene, CCLayerColor is changed to LayerColor, CCMutableArray is changed to Array (CCMutableArray has been removed), and some cc prefixes are changed to on, for example, change ccTouchesEnded to onTouchesEnded. The original shared * Singleton method is changed to get *, for example, CCDirector: sharedDirector () to Director: getInstance (), original spriteWith *, initWith *, and other static methods, change to create. For example, change CCMoveTo: actionWithDuration to MoveTo: create, CCArray: array () to Array: create. The original sets * method is changed to set *, for example, setIsVisible to setVisible, and setIsTouchEnabled to setTouchEnabled. The original ccTouchesEnded (cocos2d: CCSet * touches, cocos2d: CCEvent * event) changed onTouchesEnded (const std: vector <cocos2d: Touch *> & touches, cocos2d :: event * event)

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.