Event模块或者叫input模块,处理用户事件,比如触摸、键盘、鼠标、加速器
cc.Event:Touce、Keyboard、Mouse、Acceleration、Custom
相关类
cc.inputManager cocos2d/core/platform/CCInputManager.js
新建和触发除了Custom的其他event,让cc.eventManager分发
cc.director 新建和触发几个custom event ,让cc.eventManager分发
cc.eventManager cocos2d/core/event-manager/CCEventManager.js
管理listener的注册和删除,event 分发
注册:向cc.eventManager注册
if( 'touches' in cc.sys.capabilities ) {
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: this.onTouchBegan,
onTouchMoved: this.onTouchMoved,
onTouchEnded: this.onTouchEnded,
onTouchCancelled: this.onTouchCancelled
}, this);
注册分析:
cc.eventManager::addListener: function (listener, nodeOrPriority)
首先listener有属性
_type: 0, // Event listener type
_listenerID: null, // Event listener ID
_fixedPriority: 0
_node: null,
_fixedPriority event优先级,越小越有捕获的优先级
如果nodeOrPriority是node则_fixedPriority设置为0,该listener加到SceneGraphPriorityListeners
如果nodeOrPriority 是number而且不为0, _node=null ,该listener加到FixedPriorityListeners
SceneGraphPriorityListeners的priority都为0,捕获优先级完全跟zorder相关,
zorder越高越有优先权,同zorder addChild的越晚越有优先权,父子关系看>0 :子优先 , <0 :父优先
FixedPriorityListeners 捕获的优先级完全跟priority相关,
按照越小越有优先级,priority相等的不改变位置(越先addListener的越有优先权)
代码cc.eventManager::
_sortListenersOfFixedPriorityAsc: function (l1, l2) {
return l1._getFixedPriority() - l2._getFixedPriority();
},
回调:
onTouchBegan:function(touch, event) {
var pos = touch.getLocation();
var id = touch.getId();
回调分析:
touch参数 :onebyone 回调的touch只有一个触摸点,多点触摸相当于多次的连续回调
touch.getLocation,坐标已经经过转换,左下是0,0
touch.getId,当前在屏幕上的第几个点,0~4 最多5个点
allatonce 回调的touches是多点
var target = event.getCurrentTarget();
for (var i=0; i < touches.length;i++ ) {
var touch = touches[i];
var pos = touch.getLocation();
var id = touch.getId();
event参数:cc.EventTouch ,event包含touch or touches
包含的属性
分发流程
cc.inputManager::handleTouchesBegin
var touchEvent = new cc.EventTouch(handleTouches);
touchEvent._eventCode = cc.EventTouch.EventCode.BEGAN;
cc.eventManager.dispatchEvent(touchEvent);
cc.inputManager 新建一个EventTouch事件供cc.eventManager分发
cc.eventManager根据event对应的_listenerID罗列出listeners并排序,进行回调
ccui.Widget extend ccui.ProtectedNode
ccui.Widget向eventManager注册了touch_one_by_one listener
(web里cc.sys.capabilities 没有touches 但是可以注册onebyeone 和allatonce 何解?)
this._touchListener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: this.onTouchBegan.bind(this),
onTouchMoved: this.onTouchMoved.bind(this),
onTouchEnded: this.onTouchEnded.bind(this) //注意没有cancelled
});
cc.eventManager.addListener(this._touchListener, this);
cc.Button extend uiwidget 要实现点击事件用
button.addTouchEventListener(this.touchEvent, this);
button的触摸回调
touchEvent: function (sender, type) {
switch (type) {
case ccui.Widget.TOUCH_BEGAN:
case ccui.Widget.TOUCH_MOVED:
case ccui.Widget.TOUCH_ENDED:
case ccui.Widget.TOUCH_CANCELED:
分析
sender :该widget即button
type: ccui.Widget定义触摸widget的事件
注意uiwidget没注册onTouchCancelled 是在onTouchEnded里分发case ccui.Widget.TOUCH_ENDED和
case ccui.Widget.TOUCH_CANCELED 事件
uiwidget onTouchBegan回调函数已经判断了touch point在不在widget里
sprite 需要自己判断touch point在不在sprite的rect里面
cc.Keys CCCommon.js cocos2d/core/platform/
be continue...