您的位置:首页 > 移动开发 > Cocos引擎

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

2016-11-19 13:12 671 查看
http://blog.csdn.net/tonny_guan/article/details/38149367


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

我们可以将下面的代码:

[html] view
plain copy

 print?





listener->onTouchBegan =CC_CALLBACK_2(HelloWorld::onTouchBegan, this);  

... ...  

bool HelloWorld::onTouchBegan(Touch*touch, Event* event) {  

    ......  

    returnfalse;  

}  

 

替换为如下代码:

[html] view
plain copy

 print?





listener->onTouchBegan = [](Touch*touch, Event* event){  

   ... ...  

   return false;  

};  

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

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

 

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

[html] view
plain copy

 print?





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 = 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());  

       // 移动当前按钮精灵的坐标位置  

       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 = 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));  

       }  

   };  

   

    //添加监听器  

    EventDispatcher*eventDispatcher = Director::getInstance()->getEventDispatcher();  

    eventDispatcher->addEventListenerWithSceneGraphPriority(listener,  

                                                                                     getChildByTag(kBoxA_Tag));  

    eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),  

                                                                                     getChildByTag(kBoxB_Tag));  

    eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),  

                                                                                     getChildByTag(kBoxC_Tag));  

}  

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

[html] view
plain copy

 print?





class HelloWorld : public cocos2d::Layer  

{  

public:  

   static cocos2d::Scene* createScene();  

  virtual bool init();   

    virtualvoid onEnter();  

    virtualvoid onExit();  

     

   CREATE_FUNC(HelloWorld);  

};  

除了触摸事件还有键盘事件、鼠标事件、加速度事件和自定义事件等也都可以使用Lambda表达式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: