Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EventTarget

!#en 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 If a class cannot extend from EventTarget, it can consider implements IEventTarget interface. !#zh 事件目标是具有注册监听器、派发事件能力的类,Node 是最常见的事件目标, 但是其他类也可以继承自事件目标以获得管理监听器和派发事件的能力。 如果无法继承自 EventTarget,也可以考虑自实现 IEventTarget

Hierarchy

Index

Properties

_callbackTable

_callbackTable: ICallbackTable = createMap(true)

Methods

emit

  • emit(key: string, ...args: any[]): void
  • 派发一个指定事件,并传递需要的参数

    Parameters

    • key: string

      event type

    • Rest ...args: any[]

      Arguments when the event triggered

    Returns void

hasEventListener

  • hasEventListener(key: string, callback?: Function, target?: Object | null): boolean
  • 检查指定事件是否已注册回调。

    Parameters

    • key: string

      Event type

    • Optional callback: Function

      Callback function when event triggered

    • Default value target: Object | null = null

      Callback callee

    Returns boolean

off

  • off(type: string, callback?: Function, target?: Object): void
  • 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。

    example

    // 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');

    Parameters

    • type: string

      A string representing the event type being removed.

    • Optional callback: Function

      The callback to remove.

    • Optional target: Object

      The target (this object) to invoke the callback, if it's not given, only callback without target will be removed

    Returns void

on

  • on(type: string, callback: Function, target?: Object): undefined | Function
  • 注册事件目标的特定事件类型回调。这种类型的事件应该被 emit 触发。

    example

    eventTarget.on('fire', function () { cc.log("fire in the hole"); }, node);

    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).

    • Optional target: Object

      The target (this object) to invoke the callback, can be null

    Returns undefined | Function

    • Just returns the incoming callback so you can save the anonymous function easier.

once

  • once(type: string, callback: Function, target?: Object): undefined | Function
  • 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。

    example

    eventTarget.once('fire', function () { cc.log("this is the callback and will be invoked only once"); }, node);

    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).

    • Optional target: Object

      The target (this object) to invoke the callback, can be null

    Returns undefined | Function

removeAll

  • removeAll(keyOrTarget?: string | Object): void
  • 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。

    Parameters

    • Optional keyOrTarget: string | Object

      The event type or target with which the listeners will be removed

    Returns void

targetOff

  • targetOff(keyOrTarget?: string | Object): void
  • 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。

    Parameters

    • Optional keyOrTarget: string | Object

    Returns void

Generated using TypeDoc