【Cocos2d入門教程六】Cocos2d-x事件篇之觸摸

Cocos遊戲當中產生一個事件時,能夠有多個對象在監聽該事件,因此有優先級(Priority).優先級越高(Priority值越小),事件響應越靠前。函數

關係圖:測試



新 事件分發機制:在2.x 版本事件處理時,將要觸發的事件交給代理(delegate)處理,再經過實現代理裏面的onTouchBegan等方法接收事件,最後完成事件的響應。 而在新的事件分發機制(3.x)中,只需經過建立一個事件監聽器-用來實現各類觸發後的邏輯,而後添加到事件分發器_eventDispatcher,所 有事件監聽器由這個分發器統一管理,便可完成事件響應。this

 

事件監聽器有如下幾種:spa

  • 觸摸事件 (EventListenerTouch)
  • 鍵盤響應事件 (EventListenerKeyboard)
  • 鼠標響應事件 (EventListenerMouse)
  • 自定義事件 (EventListenerCustom)
  • 加速記錄事件 (EventListenerAcceleration)

_eventDispatcher的工做由三部分組成:代理

  • 事件分發器 EventDispatcher
  • 事件類型 EventTouch, EventKeyboard 等
  • 事件監聽器 EventListenerTouch, EventListenerKeyboard 等

監聽器實現了各類觸發後的邏輯,在適當時候由事件分發器分發事件類型,而後調用相應類型的監聽器。code

 

一.觸摸事件對象

單點觸摸:blog

 

 1 bool HelloWorld::init()
 2 {
 3     //////////////////////////////
 4     // 1. super init first
 5     if ( !Layer::init() )
 6     {
 7         return false;
 8     }
 9     Size visibleSize = Director::getInstance()->getVisibleSize();
10     
11     /* 建立精靈 */
12     Sprite* sp1 = Sprite::create("sprite1.png");
13     sp1->setPosition(Point(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
14     this->addChild(sp1);
15 
16     //註冊監聽
17     auto listener = EventListenerTouchOneByOne::create();
18     listener->setSwallowTouches(true); //阻止向下傳遞
19     listener->onTouchBegan = [](Touch* touch, Event* event){
20         /* 獲取事件綁定的精靈 */
21         auto target = static_cast<Sprite*>(event->getCurrentTarget());
22         Point pos = Director::getInstance()->convertToGL(touch->getLocationInView());
23         
24         /* 檢測是否觸摸到精靈 ⁄ */
25         if (target->getBoundingBox().containsPoint(pos))
26         {
27             /* 設置這個綁定到的精靈的透明度 */
28             target->setOpacity(100);
29             
30             return true;
31         }
32         
33         return false;
34     };
35     listener->onTouchMoved =[=](Touch* touch,Event* event)
36     {
37         /* 拖着精靈走 */
38         auto target = static_cast<Sprite*>(event->getCurrentTarget());
39         target->setPosition(touch->getLocation());
40     };
41     
42     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sp1);
43     
44     
45     return true;
46 }

 

效果圖:教程




多點觸摸:遊戲

 

 1 #include "cocos2d.h"
 2 USING_NS_CC;
 3 class HelloWorld : public cocos2d::Layer
 4 {
 5 public:
 6    
 7     static cocos2d::Scene* createScene();
 8 
 9     virtual bool init();
10     
11     void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
12     
13     void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);

 

 1 // on "init" you need to initialize your instance
 2 bool HelloWorld::init()
 3 {
 4     //////////////////////////////
 5     // 1. super init first
 6     if ( !Layer::init() )
 7     {
 8         return false;
 9     }
10     Size visibleSize = Director::getInstance()->getVisibleSize();
11     
12     /* 建立精靈 */
13     Sprite* sp1 = Sprite::create("HelloWorld.png");
14     sp1->setPosition(Point(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
15     this->addChild(sp1);
16 
17     //註冊監聽
18     auto listener = EventListenerTouchAllAtOnce::create();
19     listener->onTouchesBegan =CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
20     listener->onTouchesMoved =CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
21     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sp1);
22     
23     
24     return true;
25 }
26 
27 //多點觸摸事件響應函數
28 void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event) { CCLOG("began"); }
29 
30 void HelloWorld::onTouchesMoved(const std::vector<Touch *> &touches, cocos2d::Event *event)
31 {
32     auto sprite =static_cast<Sprite*>(event->getCurrentTarget());
33     //縮放
34     if(touches.size() > 1)
35     {
36         auto distance1 = touches[0]->getPreviousLocation().distance(touches[1]->getPreviousLocation());
37         auto distance2 = touches[0]->getLocation().distance(touches[1]->getLocation());
38         
39         float scale = sprite->getScale() * ( distance2 / distance1);
40         scale = MIN(2,MAX(0.5, scale));
41         
42         sprite->setScale(scale);
43     }
44     else
45     {
46         log("單點");
47     }
48 }

因爲多點觸摸縮放擴大不方便放示例圖出來。你們寫好代碼Run到真機測試一下。本示例已測試,絕對ok。


OK關於這一章的觸摸事件就分享至此,關於事件的加速計等教程會放在基礎教程來說。下一章將講述Cocos各類專業名詞。以後將跨越入門這道欄。進入中級開發。

相關文章
相關標籤/搜索