论坛首页 移动开发技术论坛

Cocos2d-x开发实例:使用Lambda 表达式

浏览 2105 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-07-26  

第三篇Cocos2d-x开发实例:使用Lambda 表达式

在Cocos2d-x 3.0之后提供了对C++11标准[1]的支持,其中的Lambda[2]表达式使用起来非常简洁。我们可以使用Lambda表达式重构上一节的实例。

我们可以将下面的代码:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. listener->onTouchBegan =CC_CALLBACK_2(HelloWorld::onTouchBegan, this);  
  2. ... ...  
  3. bool HelloWorld::onTouchBegan(Touch*touch, Event* event) {  
  4.     ......  
  5.     returnfalse;  
  6. }  

 

 

 

替换为如下代码:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. listener->onTouchBegan = [](Touch*touch, Event* event){  
  2.    ... ...  
  3.    return false;  
  4. };  

 

 

上面的语句[](Touch* touch, Event* event){ …}就是Lambda表达式。Lambda表达式就是JavaScript语言中的匿名函数,Java中的匿名内部类,就是在表达式中直接声明函数,而不是独立声明函数。

提示 在Lambda表达式中[]表示接下来开始定义Lambda函数,[]之后的()是Lambda函数的参数列表,{}中间就是函数体。

 

重构之后的HelloWorldScene.cpp主要修改的代码如下:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. void HelloWorld::onEnter()  
  2. {  
  3.     Layer::onEnter();  
  4.     log("HelloWorldonEnter");  
  5.      
  6.    auto listener = EventListenerTouchOneByOne::create();  
  7.    listener->setSwallowTouches(true);  
  8.      
  9.    listener->onTouchBegan = [](Touch* touch, Event* event){                                                      ①           
  10.        auto target = static_cast<Sprite*>(event->getCurrentTarget());  
  11.              PointlocationInNode = target->convertToNodeSpace(touch->getLocation());  
  12.        Size s = target->getContentSize();  
  13.        Rect rect = Rect(0, 0, s.width, s.height);  
  14.    
  15.        if (rect.containsPoint(locationInNode))  
  16.        {  
  17.             log("sprite x = %f, y = %f", locationInNode.x, locationInNode.y);  
  18.                        log("spritetag = %d", target->getTag());  
  19.                       target->runAction(ScaleBy::create(0.06f,1.06f));  
  20.             return true;  
  21.        }  
  22.        return false;  
  23.    };  
  24.      
  25.    listener->onTouchMoved = [](Touch* touch, Event* event){                                                      ②  
  26.        auto target = static_cast<Sprite*>(event->getCurrentTarget());  
  27.        // 移动当前按钮精灵的坐标位置  
  28.        target->setPosition(target->getPosition() + touch->getDelta());  
  29.    };  
  30.    
  31.    listener->onTouchEnded = [](Touch* touch, Event* event){                                                      ③  
  32.        auto target = static_cast<Sprite*>(event->getCurrentTarget());  
  33.        log("sprite onTouchesEnded.. ");  
  34.    
  35.                 PointlocationInNode = target->convertToNodeSpace(touch->getLocation());  
  36.        Size s = target->getContentSize();  
  37.        Rect rect = Rect(0, 0, s.width, s.height);  
  38.         
  39.        if (rect.containsPoint(locationInNode))  
  40.        {  
  41.             log("sprite x = %f, y = %f", locationInNode.x, locationInNode.y);  
  42.                       log("sprite tag = %d",target->getTag());  
  43.                       target->runAction(ScaleTo::create(0.06f,1.0f));  
  44.        }  
  45.    };  
  46.    
  47.     //添加监听器  
  48.     EventDispatcher*eventDispatcher = Director::getInstance()->getEventDispatcher();  
  49.     eventDispatcher->addEventListenerWithSceneGraphPriority(listener,  
  50.                                                                                      getChildByTag(kBoxA_Tag));  
  51.     eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),  
  52.                                                                                      getChildByTag(kBoxB_Tag));  
  53.     eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),  
  54.                                                                                      getChildByTag(kBoxC_Tag));  
  55. }  

 

 

上述代码第①、②、③行分别使用了Lambda表达式定义的匿名函数,具体代码不用再解释。从上面代码看使用Lambda表达式非常简洁,由于不需要单独定义回调函数,对应的头文件代码也比较简洁,HelloWorldScene.h主要代码如下:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. class HelloWorld : public cocos2d::Layer  
  2. {  
  3. public:  
  4.    static cocos2d::Scene* createScene();  
  5.   virtual bool init();   
  6.     virtualvoid onEnter();  
  7.     virtualvoid onExit();  
  8.      
  9.    CREATE_FUNC(HelloWorld);  
  10. };  

 

 

除了触摸事件还有键盘事件、鼠标事件、加速度事件和自定义事件等也都可以使用Lambda表达式。



[1] C++的最新正式标准,由C++标准委员会于2011年8月12日公布,并于2011年9月出版。2012年2月28日的国际标准草案(N3376)是最接近于现行标准的草案(编辑上的修正)。此次标准为C++98发布后13年来第一次重大修正。——引自于百度百科 http://baike.baidu.com/view/7021472.htm

 

[2] 希腊字母中的第十一个字母[∧, λ],发音 ['læmdə]。

 

 

 

更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:257760386、327403678
 
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics