Cocos2d-x 3.2 学习笔记(九)EventDispatcher事件分发机制
EventDispatcher事件分发机制先创建事件,注册到事件管理中心_eventDispatcher,通过发布事件得到响应进行回调,完成事件流。
有五种不同的事件机制:
EventListenerTouch 响应触控事件
EventListenerKeyboard 响应键盘事件
EventListenerAcceleration 响应加速器事件
EventListenMouse 响应鼠标事件
EventListenerCustom 响应自定义的事件
优先权:
1.优先级越低,越先响应事件
2.如果优先级相同,则上层的(z轴)先接收触摸事件。
事件监听器的优先级:
1.addEventListenerWithSceneGraphPriority 的事件监听器优先级是0,而且在 addEventListenerWithFixedPriority 中的事件监听器的优先级不可以设置为 0,因为这个是保留给 SceneGraphPriority 使用的。
2.另外,有一点非常重要,FixedPriority listener添加完之后需要手动remove,而SceneGraphPriority listener是跟node绑定的,在node的析构函数中会被移除。移除方法:dispatcher->removeEventListener(listener);
事件管理单例获取:
_eventDispatcher是Node的属性,通过它管理当前节点(场景、层、精灵等)的所有事件的分发。但它本身是一个单例模式值的引用,在Node的构造函数中,通过Director::getInstance()->getEventDispatcher(); 获取,有了这个属性,就能方便的处理事件。
触摸事件:
void EventDispatcherTest::funEventTouch(Sprite* sprite)
{
this->_eventDispatcher->removeAllEventListeners(); auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(EventDispatcherTest::onTouchBeganss,this);
listener->onTouchMoved = CC_CALLBACK_2(EventDispatcherTest::onTouchMovedss,this);
listener->onTouchEnded = CC_CALLBACK_2(EventDispatcherTest::onTouchEndedss,this);
listener->onTouchCancelled = CC_CALLBACK_2(EventDispatcherTest::onTouchCancelledss,this);
listener->setSwallowTouches(true);//是否向下传递
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,sprite);
} bool EventDispatcherTest::onTouchBeganss(Touch* touch,Event* ev)
{
auto target = static_cast<Sprite*>(ev->getCurrentTarget()); Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(, , s.width, s.height);
//判断触摸区域是否在目标上
if (rect.containsPoint(locationInNode))
{
label->setString("onTouchBegan......");
target->setOpacity();
return true;
}
return false;
}
void EventDispatcherTest::onTouchMovedss(Touch* touch,Event* ev)
{
auto target = static_cast<Sprite*>(ev->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
label->setString("onTouchMoved......");
}
void EventDispatcherTest::onTouchEndedss(Touch* touch,Event* ev)
{
auto target = static_cast<Sprite*>(ev->getCurrentTarget());
target->setOpacity();
label->setString("onTouchEnded......");
}
void EventDispatcherTest::onTouchCancelledss(Touch* touch,Event* ev)
{
label->setString("onTouchCancelled......");
}
键盘事件
void EventDispatcherTest::funEventKeyboard()
{
this->_eventDispatcher->removeAllEventListeners(); auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(EventDispatcherTest::onKeyPressedss,this);
listener->onKeyReleased = CC_CALLBACK_2(EventDispatcherTest::onKeyReleasedss,this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
} void EventDispatcherTest::onKeyPressedss(EventKeyboard::KeyCode keycode,Event* ev)
{
char txt[] = {};
sprintf_s(txt,"key %d is Pressed!",(int)keycode);
label->setString(txt);
} void EventDispatcherTest::onKeyReleasedss(EventKeyboard::KeyCode keycode,Event* ev)
{
label->setString("key is Released!");
}
鼠标事件
void EventDispatcherTest::funEventMouse(Sprite* sprite)
{
this->_eventDispatcher->removeAllEventListeners(); auto listener = EventListenerMouse::create();
listener->onMouseDown = CC_CALLBACK_1(EventDispatcherTest::onMouseDownss,this);
listener->onMouseMove = CC_CALLBACK_1(EventDispatcherTest::onMouseMovess,this);
listener->onMouseUp = CC_CALLBACK_1(EventDispatcherTest::onMouseUpss,this);
listener->onMouseScroll = CC_CALLBACK_1(EventDispatcherTest::onMouseScrollss,this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,sprite);
} void EventDispatcherTest::onMouseDownss(Event* ev)
{
label->setString("onMouseDown!");
}
void EventDispatcherTest::onMouseMovess(Event* ev)
{
label->setString("onMouseMove!");
}
void EventDispatcherTest::onMouseUpss(Event* ev)
{
label->setString("onMouseUp!");
}
void EventDispatcherTest::onMouseScrollss(Event* ev)
{
label->setString("onMouseScroll!");
}
自定义事件
//自定義事件
funEventCustom();
//2秒後派發一次自定義事件,測試
scheduleOnce(schedule_selector(EventDispatcherTest::dispatcherCustomEvents),2.0f);
void EventDispatcherTest::funEventCustom()
{
auto listener = EventListenerCustom::create("custom_event_1",CC_CALLBACK_1(EventDispatcherTest::onEventCustom,this));
this->_eventDispatcher->addEventListenerWithFixedPriority(listener,);//添加到事件分發器
} void EventDispatcherTest::dispatcherCustomEvents(float at)
{
//派發事件custom_event_1 事件內容為字符串custom event test!
this->_eventDispatcher->dispatchCustomEvent("custom_event_1","custom event test!");
} void EventDispatcherTest::onEventCustom(EventCustom* event)
{
auto data = static_cast<char*>(event->getUserData());
label->setString(data);
}
加速器事件
除了触摸,移动设备上一个很重要的输入源是设备的方向,因此大多数设备都配备了加速计,用于测量设备静止或匀速运动时所受到的重力方向。
重力感应来自移动设备的加速计,通常支持X,Y和Z三个方向的加速度感应,所以又称为三向加速计。在实际应用中,可以根据3个方向的力度大小来计算手机倾斜的角度或方向。
加速计监听器EventListenerAcceleration,其静态create方法中有个Acceleration的参数。Acceleration是一个类,包含了加速计获得的3个方向的加速度。
void EventDispatcherTest::funEventAcceleration()
{
//啟動硬件設備
Device::setAccelerometerEnabled(true); auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(EventDispatcherTest::onAcceleration,this));
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
} void EventDispatcherTest::onAcceleration(Acceleration* acc,Event* event)
{
char str[]={};
sprintf_s(str,"x:%2d,y:%2d,z:%2d,timestamp:%2d",acc->x,acc->y,acc->z,acc->timestamp);
log(str);
}
示例代码
#ifndef __EventDispatcherTest__
#define __EventDispatcherTest__ #include "cocos2d.h" USING_NS_CC; class EventDispatcherTest : public Layer
{
public:
CREATE_FUNC(EventDispatcherTest);
virtual bool init();
static Scene* createScene();
LabelTTF* label;
void funEventTouch(Sprite* sprite);
void funEventKeyboard();
void funEventMouse(Sprite* sprite);
void funEventCustom();
void funEventAcceleration(); bool onTouchBeganss(Touch* touch,Event* ev);
void onTouchMovedss(Touch* touch,Event* ev);
void onTouchEndedss(Touch* touch,Event* ev);
void onTouchCancelledss(Touch* touch,Event* ev); void onKeyPressedss(EventKeyboard::KeyCode keycode,Event* ev);
void onKeyReleasedss(EventKeyboard::KeyCode keycode,Event* ev); void onMouseDownss(Event* ev);
void onMouseMovess(Event* ev);
void onMouseUpss(Event* ev);
void onMouseScrollss(Event* ev); void dispatcherCustomEvents(float at);
void onEventCustom(EventCustom* event); void onAcceleration(Acceleration* acc,Event* event);
}; #endif
EventDispatcherTest.h
#include "EventDispatcherTest.h"
Scene* EventDispatcherTest::createScene()
{
auto scene = Scene::create();
auto layer = EventDispatcherTest::create();
scene->addChild(layer);
return scene;
} bool EventDispatcherTest::init()
{ Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
label = LabelTTF::create("Hello World", "Arial", );
label->setPosition(Vec2(origin.x + visibleSize.width/,
origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, ); auto sprite = Sprite::create("MagentaSquare.png",Rect(,,,));
sprite->setPosition(visibleSize/);
this->addChild(sprite); //funEventTouch(sprite); //觸摸事件
//funEventKeyboard(); //鍵盤事件
//funEventMouse(sprite);//鼠標事件
//funEventAcceleration();//加速器事件 //自定義事件
funEventCustom();
//2秒後派發一次自定義事件,測試
scheduleOnce(schedule_selector(EventDispatcherTest::dispatcherCustomEvents),2.0f);
return true;
} void EventDispatcherTest::funEventTouch(Sprite* sprite)
{
this->_eventDispatcher->removeAllEventListeners(); auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(EventDispatcherTest::onTouchBeganss,this);
listener->onTouchMoved = CC_CALLBACK_2(EventDispatcherTest::onTouchMovedss,this);
listener->onTouchEnded = CC_CALLBACK_2(EventDispatcherTest::onTouchEndedss,this);
listener->onTouchCancelled = CC_CALLBACK_2(EventDispatcherTest::onTouchCancelledss,this);
listener->setSwallowTouches(true);//是否向下传递
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,sprite);
} bool EventDispatcherTest::onTouchBeganss(Touch* touch,Event* ev)
{
auto target = static_cast<Sprite*>(ev->getCurrentTarget()); Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(, , s.width, s.height); if (rect.containsPoint(locationInNode))
{
label->setString("onTouchBegan......");
target->setOpacity();
return true;
}
return false;
}
void EventDispatcherTest::onTouchMovedss(Touch* touch,Event* ev)
{
auto target = static_cast<Sprite*>(ev->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
label->setString("onTouchMoved......");
}
void EventDispatcherTest::onTouchEndedss(Touch* touch,Event* ev)
{
auto target = static_cast<Sprite*>(ev->getCurrentTarget());
target->setOpacity();
label->setString("onTouchEnded......");
}
void EventDispatcherTest::onTouchCancelledss(Touch* touch,Event* ev)
{
label->setString("onTouchCancelled......");
} void EventDispatcherTest::funEventKeyboard()
{
this->_eventDispatcher->removeAllEventListeners(); auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(EventDispatcherTest::onKeyPressedss,this);
listener->onKeyReleased = CC_CALLBACK_2(EventDispatcherTest::onKeyReleasedss,this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
} void EventDispatcherTest::onKeyPressedss(EventKeyboard::KeyCode keycode,Event* ev)
{
char txt[] = {};
sprintf_s(txt,"key %d is Pressed!",(int)keycode);
label->setString(txt);
} void EventDispatcherTest::onKeyReleasedss(EventKeyboard::KeyCode keycode,Event* ev)
{
label->setString("key is Released!");
} void EventDispatcherTest::funEventMouse(Sprite* sprite)
{
this->_eventDispatcher->removeAllEventListeners(); auto listener = EventListenerMouse::create();
listener->onMouseDown = CC_CALLBACK_1(EventDispatcherTest::onMouseDownss,this);
listener->onMouseMove = CC_CALLBACK_1(EventDispatcherTest::onMouseMovess,this);
listener->onMouseUp = CC_CALLBACK_1(EventDispatcherTest::onMouseUpss,this);
listener->onMouseScroll = CC_CALLBACK_1(EventDispatcherTest::onMouseScrollss,this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,sprite);
} void EventDispatcherTest::onMouseDownss(Event* ev)
{
label->setString("onMouseDown!");
}
void EventDispatcherTest::onMouseMovess(Event* ev)
{
label->setString("onMouseMove!");
}
void EventDispatcherTest::onMouseUpss(Event* ev)
{
label->setString("onMouseUp!");
}
void EventDispatcherTest::onMouseScrollss(Event* ev)
{
label->setString("onMouseScroll!");
} void EventDispatcherTest::funEventCustom()
{
auto listener = EventListenerCustom::create("custom_event_1",CC_CALLBACK_1(EventDispatcherTest::onEventCustom,this));
this->_eventDispatcher->addEventListenerWithFixedPriority(listener,);//添加到事件分發器
} void EventDispatcherTest::dispatcherCustomEvents(float at)
{
//派發事件custom_event_1 事件內容為字符串custom event test!
this->_eventDispatcher->dispatchCustomEvent("custom_event_1","custom event test!");
} void EventDispatcherTest::onEventCustom(EventCustom* event)
{
auto data = static_cast<char*>(event->getUserData());
label->setString(data);
} void EventDispatcherTest::funEventAcceleration()
{
//啟動硬件設備
Device::setAccelerometerEnabled(true); auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(EventDispatcherTest::onAcceleration,this));
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
} void EventDispatcherTest::onAcceleration(Acceleration* acc,Event* event)
{
char str[]={};
sprintf_s(str,"x:%2d,y:%2d,z:%2d,timestamp:%2d",acc->x,acc->y,acc->z,acc->timestamp);
log(str);
}
EventDispatcherTest.cpp
Cocos2d-x 3.2 学习笔记(九)EventDispatcher事件分发机制的更多相关文章
- Cocos2d-x 学习笔记(15.2) EventDispatcher 事件分发机制 dispatchEvent(event)
1. 事件分发方法 EventDispatcher::dispatchEvent(Event* event) 首先通过_isEnabled标志判断事件分发是否启用. 执行 updateDirtyFla ...
- 多线程学习笔记九之ThreadLocal
目录 多线程学习笔记九之ThreadLocal 简介 类结构 源码分析 ThreadLocalMap set(T value) get() remove() 为什么ThreadLocalMap的键是W ...
- MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九
<Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次 SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...
- python3.4学习笔记(九) Python GUI桌面应用开发工具选择
python3.4学习笔记(九) Python GUI桌面应用开发工具选择 Python GUI开发工具选择 - WEB开发者http://www.admin10000.com/document/96 ...
- Go语言学习笔记九: 指针
Go语言学习笔记九: 指针 指针的概念是当时学C语言时了解的.Go语言的指针感觉与C语言的没啥不同. 指针定义与使用 指针变量是保存内存地址的变量.其他变量保存的是数值,而指针变量保存的是内存地址.这 ...
- go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin)
目录 go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin) zipkin使用demo 数据持久化 go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin ...
- iOS学习笔记之触摸事件&UIResponder
iOS学习笔记之触摸事件&UIResponder 触摸事件 与触摸事件相关的四个方法如下: 一根手指或多根手指触摸屏幕 -(void)touchesBegan:(NSSet *)touches ...
- Android程序员事件分发机制学习笔记
通过问题来学习一个东西是很好的方法.学习Android中View的事件体系,我也通过给自己提问题,在解决问题的同时也就知道了其中原理. 首先来几个问题起步: 什么是事件?什么是事件分发机制? 在我们通 ...
- 【Unity游戏开发】用C#和Lua实现Unity中的事件分发机制EventDispatcher
一.简介 最近马三换了一家大公司工作,公司制度规范了一些,因此平时的业余时间多了不少.但是人却懒了下来,最近这一个月都没怎么研究新技术,博客写得也是拖拖拉拉,周六周天就躺尸在家看帖子.看小说,要么就是 ...
随机推荐
- 从csdn博客搬家过来,请多关照!
之前一直在csdn博客,也写了不少关于java的文章,主要是学习java上的一些问题.想通过这种方式来加深对问题的认知,同时也可以帮助到志同道合的人,一起在编程的道路上共进,共勉.
- 对bit、byte、TByte、Char、string、进制的认识
在学校老师就教1byte = 8bit,一个Byte在内存中占8个房间.每个房间都有门牌号.找到内存中的内容就找门牌号,寻址什么的,虽然在听,但是脑袋里一头雾水,到现在只知道会用就行,但原理也不是那么 ...
- 基本XML解析---编写
#import "ViewController.h" #import "DDXML.h" @interface ViewController () @end @ ...
- springMVC注解启用及优化
使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用注解方式. Springmvc的注解是在2.5版本后有了注解,如何开启注解配置文件 Web.xml文件中不 ...
- nginx.conf各参数的意义
搬运+翻译至 http://qiita.com/syou007/items/3e2d410bbe65a364b603 /etc/nginx/nginx.conf 记录各个参数的意义 user user ...
- iOS集成丁香园DXY OAuth 登陆 swift代码示例
问:iOS集成OAuth登陆分几步? 答:和把大象放冰箱里一样. 第一步:打开webview,跳转到登陆页面: let url = "https://auth.dxy.cn/conn/oau ...
- 【腾讯Bugly干货分享】QQ电话适配iOS10 Callkit框架
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/58009392302e4725036142fc Dev Club 是一个交流移动 ...
- java 多线程(threadlocal)
package com.example; import java.util.Random; public class App { public static class MyRunnable1 imp ...
- [ZigBee] 12、ZigBee之看门狗定时器——饿了就咬人的GOOD DOG
引言:硬件中的看门狗,不是门卫的意思,而是一只很凶的狗!如果你不按时喂它,它就会让系统重启!这反而是我们想要的功能~ 1.看门狗概述 看门狗定时器(WDT,Watch Dog Timer)是单片机的一 ...
- JavaScript获取两个数之间的任意随机数
通过JavaScript的Math.random()方法可以获取0到1之间的任意随机数,那如何获取任意给定的两个数之间的随机数呢?如获取2和5之间的随机数,5和10之间的随机数等. 由于Math.ra ...