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

本文介绍如何在Cocos2d-x中利用C++11的Lambda表达式简化触摸事件处理,通过示例代码展示了Lambda表达式的简洁性和易用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/tonny_guan/article/details/38149367

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

我们可以将下面的代码:

[html]  view plain  copy
 print ? 在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 plain  copy
 print ? 在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 plain  copy
 print ? 在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 plain  copy
 print ? 在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表达式。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值