EventTarget
类型
定义于: https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:99
模块: cc
事件目标是事件触发时,分派的事件对象,Node 是最常见的事件目标,
但是其他对象也可以是事件目标。
索引
方法
on
注册事件目标的特定事件类型回调。off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。targetOff
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent
分发事件到事件流中。emit
该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。
Details
方法
on
注册事件目标的特定事件类型回调。
meta | description |
---|---|
返回 | Function |
定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:217 |
参数列表
- type String A string representing the event type to listen for.
- callback Function The callback that will be invoked when the event is dispatched.
The callback is ignored if it is a duplicate (the callbacks are unique).
- event Event event
- target Object The target (this object) to invoke the callback, can be null
- useCapture Boolean When set to true, the capture argument prevents callback
from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
示例
node.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
meta | description |
---|---|
定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:274 |
参数列表
- type String A string representing the event type being removed.
- callback Function The callback to remove.
- target Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
- useCapture Boolean Specifies whether the callback being removed was registered as a capturing callback or not.
If not specified, useCapture defaults to false. If a callback was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing callback does not affect a non-capturing version of the same listener, and vice versa.
示例
// register touchEnd eventListener
var touchEnd = node.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
// remove touch end event listener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);
// remove all touch end event listeners
node.off(cc.Node.EventType.TOUCH_END);
targetOff
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
meta | description |
---|---|
定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:329 |
参数列表
- target Object The target to be searched for all related listeners
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
meta | description |
---|---|
定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351 |
参数列表
- type String A string representing the event type to listen for.
- callback Function The callback that will be invoked when the event is dispatched.
The callback is ignored if it is a duplicate (the callbacks are unique).
- event Event event
- target Object The target (this object) to invoke the callback, can be null
- useCapture Boolean When set to true, the capture argument prevents callback
from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
示例
node.once(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
dispatchEvent
分发事件到事件流中。
meta | description |
---|---|
定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396 |
参数列表
- event Event The Event object that is dispatched into the event flow
emit
该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。
meta | description |
---|---|
定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410 |
参数列表
- message String the message to send
- detail Any whatever argument the message needs