EventTarget Class

Extends CallbacksInvoker

Defined in: https://github.com/cocos-creator/engine/blob/a2f4b48f64e8117cf0d5a93229bfe31932c42384/cocos2d/core/event/event-target.js:35

Module: cc

EventTarget is an object to which an event is dispatched when something has occurred. Entity are the most common event targets, but other objects can be event targets too.

Event targets are an important part of the Fireball event model. The event target serves as the focal point for how events flow through the scene graph. When an event such as a mouse click or a keypress occurs, Fireball dispatches an event object into the event flow from the root of the hierarchy. The event object then makes its way through the scene graph until it reaches the event target, at which point it begins its return trip through the scene graph. This round-trip journey to the event target is conceptually divided into three phases:

  • The capture phase comprises the journey from the root to the last node before the event target's node
  • The target phase comprises only the event target node
  • The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the tree See also: http://www.w3.org/TR/DOM-Level-3-Events/#event-flow

Event targets can implement the following methods:

  • _getCapturingTargets
  • _getBubblingTargets

Index

Methods
  • hasEventListener Checks whether the EventTarget object has any callback registered for a specific type of event.
  • on Register an callback of a specific event type on the EventTarget.
  • off Removes the listeners previously registered with the same type, callback, target and or useCapture,...
  • targetOff Removes all callbacks previously registered with the same target (passed as parameter).
  • once Register an callback of a specific event type on the EventTarget,...
  • dispatchEvent Send an event with the event object.
  • clear Destroy all callbackInfos.
  • removeAll Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
  • emit Trigger an event directly with the event name and necessary arguments.

Details

Methods

hasEventListener

Checks whether the EventTarget object has any callback registered for a specific type of event.

meta description
Returns Boolean
Defined in cocos2d/core/event/event-target.js:69
Parameters
  • type String The type of event.
on

Register an callback of a specific event type on the EventTarget. This type of event should be triggered via emit.

meta description
Returns Function
Defined in cocos2d/core/event/event-target.js:77
Parameters
  • 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).
    
    • arg1 Any arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 Any arg5
  • target Object The target (this object) to invoke the callback, can be null
Examples
eventTarget.on('fire', function () {
    cc.log("fire in the hole");
}, node);
off

Removes the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.

meta description
Defined in cocos2d/core/event/event-target.js:119
Parameters
  • 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
Examples
// register fire eventListener
var callback = eventTarget.on('fire', function () {
    cc.log("fire in the hole");
}, target);
// remove fire event listener
eventTarget.off('fire', callback, target);
// remove all fire event listeners
eventTarget.off('fire');
targetOff

Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.

meta description
Defined in cocos2d/core/event/event-target.js:163
Parameters
  • target Object The target to be searched for all related listeners
once

Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.

meta description
Defined in cocos2d/core/event/event-target.js:182
Parameters
  • 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).
    
    • arg1 Any arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 Any arg5
  • target Object The target (this object) to invoke the callback, can be null
Examples
eventTarget.once('fire', function () {
    cc.log("this is the callback and will be invoked only once");
}, node);
dispatchEvent

Send an event with the event object.

meta description
Defined in cocos2d/core/event/event-target.js:208
Parameters
clear

Destroy all callbackInfos.

meta description
Defined in cocos2d/core/event/event-target.js:221
removeAll

Removes all callbacks registered in a certain event type or all callbacks registered with a certain target

meta description
Defined in cocos2d/core/platform/callbacks-invoker.js:235
Parameters
  • keyOrTarget String | Object The event key to be removed or the target to be removed
emit

Trigger an event directly with the event name and necessary arguments.

meta description
Defined in cocos2d/core/platform/callbacks-invoker.js:309
Parameters
  • key String event type
  • arg1 Any First argument
  • arg2 Any Second argument
  • arg3 Any Third argument
  • arg4 Any Fourth argument
  • arg5 Any Fifth argument
Examples
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);

results matching ""

    No results matching ""