码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x事件监听机制

时间:2015-05-28 00:37:44      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

由于没有涉及到需要自定义监听事件以及暂时没有监听移动设备加速计的需求,所以暂时没有测试

 

1.监听触摸屏幕的事件

技术分享

 

2.监听键盘事件

技术分享

 

3.监听鼠标事件

技术分享

 

4.用户自定义事件(实际上是将时间分发器当成消息中心发布消息触发响应事件,似乎模拟了观察者模式)

技术分享

 

5.加速计事件

这个事件大致看了一下,使用方法很简单,但是在PC上看不到结果,可是很幸运的没有直接崩溃:

技术分享

 

所有代码如下:

  1 #include "TouchScene.h"
  2 #include <string>
  3 
  4 USING_NS_CC;
  5 
  6 Scene* TouchScene::createScene(){
  7     auto scene = Scene::create();
  8     auto layer = TouchScene::create();
  9     scene->addChild(layer);
 10     return scene;
 11 }
 12 
 13 bool TouchScene::init(){
 14     if (!Layer::init()){
 15         return false;
 16     }
 17     auto visibleSize = Director::getInstance()->getVisibleSize();
 18 
 19     auto sprite1 = Sprite::create("sprite.png");
 20     sprite1->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
 21 
 22     auto sprite2 = Sprite::create("sprite2.png");
 23     sprite2->setPosition(Point(visibleSize.width / 2 - 100, visibleSize.height / 2));
 24 
 25     auto sprite3 = Sprite::create("sprite3.png");
 26     sprite3->setPosition(Point(visibleSize.width / 2 + 100, visibleSize.height / 2));
 27 
 28     this->addChild(sprite1);
 29     this->addChild(sprite2);
 30     this->addChild(sprite3);
 31 
 32     /*触摸事件监听*/
 33     auto listener1 = EventListenerTouchOneByOne::create();
 34     listener1->setSwallowTouches(true);
 35     listener1->onTouchBegan = [](Touch *touch,Event *event){
 36         auto target = static_cast<Sprite*>(event->getCurrentTarget());
 37         Point locationInNode = target->convertToNodeSpace(touch->getLocation());
 38         Size s = target->getContentSize();
 39         Rect rect = Rect(0, 0, s.width, s.height);
 40         if (rect.containsPoint(locationInNode)){
 41             log("%d,%d", locationInNode.x, locationInNode.y);
 42             return true;
 43         }
 44         return false;
 45     };
 46     listener1->onTouchMoved = [](Touch* touch, Event* event){
 47         log("MOVE");
 48     };
 49     listener1->onTouchEnded = [](Touch* touch, Event* event){
 50         log("END");
 51     };
 52 
 53     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
 54     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
 55     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
 56 
 57 
 58     /*键盘事件监听*/
 59     auto listener2 = EventListenerKeyboard::create();
 60     listener2->onKeyPressed = CC_CALLBACK_2(TouchScene::onKeyPressed, this);
 61     listener2->onKeyReleased = CC_CALLBACK_2(TouchScene::onKeyReleased, this);
 62 
 63     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, this);
 64 
 65     /*鼠标事件监听*/
 66     auto listener3 = EventListenerMouse::create();
 67     listener3->onMouseDown = [=](Event *event){
 68         auto e = static_cast<EventMouse *>(event);
 69         log("%d", e->getMouseButton());
 70     };
 71     listener3->onMouseUp = [=](Event *event){
 72         auto e = static_cast<EventMouse *>(event);
 73         log("%d", e->getMouseButton());
 74     };
 75     listener3->onMouseMove = [=](Event *event){
 76         auto e = static_cast<EventMouse *>(event);
 77         log("%d", e->getMouseButton());
 78     };
 79     listener3->onMouseScroll = [=](Event *event){
 80         auto e = static_cast<EventMouse *>(event);
 81         log("%d", e->getMouseButton());
 82     };
 83 
 84     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener3, this);
 85 
 86     /*用户自定义事件的分发*/
 87     auto listener4 = EventListenerCustom::create("custom_event1", [=](EventCustom *event){
 88         std::string str("Custom event 1 received,");
 89         char *buf = static_cast<char*>(event->getUserData());
 90         str += buf;
 91         str += " times";
 92         log(str.c_str());
 93     });
 94     _eventDispatcher->addEventListenerWithFixedPriority(listener4, 1);
 95 
 96     char *buf = "event 1";
 97     EventCustom event("custom_event1");
 98     event.setUserData(buf);
 99     _eventDispatcher->dispatchEvent(&event);
100 
101     /*加速计事件*/
102     Device::setAccelerometerEnabled(true);
103     auto listener5 = EventListenerAcceleration::create([=](Acceleration *acc,Event *event){
104         log("Acceleration event");
105     });
106     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener5, this);
107 
108     return true;
109 }
110 
111 void TouchScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event){
112     log("%d",keyCode);
113 }
114 
115 void TouchScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event *event){
116     log("%d", keyCode);
117 }

 

以上。

Cocos2d-x事件监听机制

标签:

原文地址:http://www.cnblogs.com/lhyz/p/4534796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!