Cocos2d-x development example: Using lambda expressions

Source: Internet
Author: User
The support for C ++ 11 standard [1] is provided after Cocos2d-x 3.0, where Lambda [2] expressions are very simple to use. We can use lambda expressions to refactor the instance in the previous section.

We can use the following code:

listener->onTouchBegan =CC_CALLBACK_2(HelloWorld::onTouchBegan, this);... ...bool HelloWorld::onTouchBegan(Touch*touch, Event* event) {    ......    returnfalse;}

 

Replace it with the following code:

listener->onTouchBegan = [](Touch*touch, Event* event){   ... ...   return false;};

The preceding statement [] (touch * touch, event * event ){...} It is a Lambda expression. Lambda expressions are anonymous functions in Javascript, while Java's anonymous internal classes are directly declared functions in expressions, rather than independent declarations.

The prompt [] in the lambda expression indicates that the lambda function is defined next. () after [] indicates the list of Lambda function parameters, and {} indicates the function body in the middle.

 

The modified code of helloworldscene. cpp after reconstruction is as follows:

Void helloworld: onenter () {layer: onenter (); log ("helloworldonenter"); Auto listener = eventlistenertouchonebyone: Create (); listener-> setswallowtouches (true ); listener-> ontouchbegan = [] (touch * touch, event * event) {① auto target = static_cast <sprite *> (Event-> getcurrenttarget ()); pointlocationinnode = target-> converttonodespace (touch-> getlocation (); Size S = target-> getcontentsize (); rect = rect (0, 0, S. width, S. height); If (rect. containspoint (locationinnode) {log ("Sprite x = % F, y = % F", locationinnode. x, locationinnode. y); log ("spritetag = % d", target-> gettag (); Target-> runaction (scaleby: Create (0.06f, 1.06f); Return true ;} return false ;}; listener-> ontouchmoved = [] (touch * touch, event * event) {② auto target = static_cast <sprite *> (Event-> getcurrenttarget ()); // move the Coordinate Position of the current button genie target-> setposition (Target-> getposition () + touch-> getdelta ());}; listener-> ontouchended = [] (touch * touch, event * event) {③ auto target = static_cast <sprite *> (Event-> getcurrenttarget ()); log ("Sprite ontouchesended .. "); pointlocationinnode = target-> converttonodespace (touch-> getlocation (); Size S = target-> getcontentsize (); rect = rect (0, 0, S. width, S. height); If (rect. containspoint (locationinnode) {log ("Sprite x = % F, y = % F", locationinnode. x, locationinnode. y); log ("Sprite tag = % d", target-> gettag (); Target-> runaction (scaleto: Create (0.06f, 1.0f ));}}; // Add the listener eventdispatcher * eventdispatcher = Director: getinstance ()-> geteventdispatcher (); eventdispatcher-> addeventlistenerwithscenegraphpriority (listener, getchildbytag (kboxtag )); eventdispatcher-> listener (listener-> clone (), getchildbytag (kboxb_tag); eventdispatcher-> listener (listener-> clone (), getchildbytag (kboxc_tag ));}

The above Code uses anonymous functions defined by lambda expressions in rows ①, ②, and ③. The specific code does not need to be explained. From the code above, the use of lambda expressions is very concise. Because callback functions do not need to be defined separately, the corresponding header file code is also concise. The main code of helloworldscene. H is as follows:

class HelloWorld : public cocos2d::Layer{public:   static cocos2d::Scene* createScene();  virtual bool init();     virtualvoid onEnter();    virtualvoid onExit();      CREATE_FUNC(HelloWorld);};

In addition to touch events, Keyboard Events, mouse events, acceleration events, and custom events, lambda expressions can also be used.


[1] C ++'s latest formal standards were published by the C ++ Standards Committee on August 12, 2011 and published on September 2011. The draft international standard (n3376) of February 28, 2012 was the most recent draft (revised) of the current standard ). This standard is the first major amendment to C ++ 98 in the 13 years after its release. -- From Baidu encyclopedia http://baike.baidu.com/view/7021472.htm

 

[2] [upper, λ], 11th of the Greek letters, and ['l? MD?].



More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ development" book exchange discussion site: http://www.c Ocoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678


Cocos2d-x development example: Using lambda expressions

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.