cocos2dx 3.x(點擊螢幕移動精靈,拖動精靈)不需要寫回呼函數

來源:互聯網
上載者:User

標籤:blog   log   ast   style   init   dev   pac   .cpp   als   

 1 // 2 //  MainScene.cpp 3 //  helloworld 4 // 5 //  Created by apple on 16/9/19. 6 // 7 // 8  9 #include "MainScene.hpp"10 Scene * MainScene::createScene()11 {12      auto scene = Scene::create();13     //建立層14     MainScene *layer = MainScene::create();15     scene->addChild(layer);16     return scene;17 }18 bool MainScene::init(){19     if (!Layer::init()) {20         return false;21     }22     23     24     sprite = Sprite::create("hu.png");25     sprite ->setPosition(50, 50);26     sprite->setAnchorPoint(Vec2(0.5, 0.5));27     this->addChild(sprite);// 添加到層28     29   30     31     //聲明32     auto listener1 = EventListenerTouchOneByOne::create();33     listener1->setSwallowTouches(true);34     35     //通過 lambda 運算式 直接實現觸摸事件的回掉方法36     listener1->onTouchBegan = [](Touch* touch, Event* event){37         auto target = static_cast<Sprite*>(event->getCurrentTarget());38         39         Point locationInNode = target->convertToNodeSpace(touch->getLocation());40         Size s = target->getContentSize();41         Rect rect = Rect(0, 0, s.width, s.height);42         43         if (rect.containsPoint(locationInNode))44         {45             log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);46             target->setOpacity(180);//點擊的時候精靈顏色變暗,255為最大值,0最小47             return true;48         }49         return false;50     };51     52     53     54     listener1->onTouchMoved = [](Touch* touch, Event* event){55         auto target = static_cast<Sprite*>(event->getCurrentTarget());56         target->setPosition(target->getPosition() + touch->getDelta());57     };58     59     listener1->onTouchEnded = [=](Touch* touch, Event* event){60         auto target = static_cast<Sprite*>(event->getCurrentTarget());61         log("sprite onTouchesEnded.. ");62         target->setOpacity(255);//手勢鬆開時使精靈恢複原來的顏色63     };64     65     66     //將觸摸事件綁定到精靈身上67     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite);68     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite);69     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite);70     71     return true;72 }

 

cocos2dx 3.x(點擊螢幕移動精靈,拖動精靈)不需要寫回呼函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.