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
一.简介 最近马三换了一家大公司工作,公司制度规范了一些,因此平时的业余时间多了不少.但是人却懒了下来,最近这一个月都没怎么研究新技术,博客写得也是拖拖拉拉,周六周天就躺尸在家看帖子.看小说,要么就是 ...
随机推荐
- ps(process status)
1.PS ps -a(all):显示现行终端机下的所有进程,包括其他用户的进程: ps -ax: 同时加上x参数会显示没有控制终端的进程. ps -j:显示与作业有关的信息:会话ID.进程组ID等 ...
- storysnail的Windows串口编程笔记
storysnail的Windows串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据 ...
- @MappedSuperclass的用法
实体类baseEntity.java package com.rock.cft.hibernate; import java.util.Date; import javax.persistence.G ...
- PDO vs. MySQLi 选择哪一个?(PDO vs. MySQLi: Which Should You Use?)-转载
用Php访问数据的时候,你选择MySQLi和PDO,在选择之前,你应该知道些什么呢? 这篇文章将会介绍这两种方式的不同点,数据库的支持.稳定性.性能等问题. 概述 PDO MySQLi Datab ...
- C++11新特性总结 (一)
1. 概述 最近在看C++ Primer5 刚好看到一半,总结一下C++11里面确实加了很多新东西,如果没有任何了解,别说自己写了,看别人写的代码估计都会有些吃力.C++ Primer5是学习C++1 ...
- Docker Machine v1.11.2安装与使用
官方文档:Docker Machine 官方文档:Docker Toolbox boot2docker安装包官网下载链接:Docker Toolbox-1.11.1b.exe 此安装包包含的“boot ...
- angular ng-repeat+sortable 拖拽demo
由于项目需求,需要使用angular 实现列表的增.删.改,并且列表支持拖拽. 看了下angular-ui 里面的sortable组件,使用起来也是非常简单,几十行代码就完成了所需功能. 我现在懒得想 ...
- 返本求源——DOM元素的特性与属性
抛砖引玉 很多前端类库(比如dojo与JQuery)在涉及dom操作时都会见到两个模块:attr.prop.某天代码复查时,见到一段为某节点设置文本的代码: attr.set(node, 'inner ...
- 架构设计:前后端分离之Web前端架构设计
在前面的文章里我谈到了前后端分离的一些看法,这个看法是从宏观的角度来思考的,没有具体的落地实现,今天我将延续上篇文章的主题,从纯前端的架构设计角度谈谈前后端分离的一种具体实现方案,该方案和我原来设想有 ...
- html表格相关
<html> <head> <style type="text/css"> thead {color:green} tbody {color:b ...