Cocos Creator API

1.2.0

Cocos Creator is a highly customizable game development tool that utilizes the power of cocos2d-x.

EventTarget

模块: cc

事件目标是事件触发时,分派的事件对象,Node 是最常见的事件目标, 但是其他对象也可以是事件目标。

方法

on
(
  • type
  • callback
  • target
  • useCapture
)
Function

注册事件目标的特定事件类型回调,仅仅是 addEventListener 的别名。

名称 类型 描述
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).

target Object

The target 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.

返回:

类型: Function

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

示例:

node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);

off
(
  • type
  • callback
  • target
  • useCapture
)

删除之前与同类型,回调,目标或 useCapture 注册的回调,仅仅是 removeEventListener 的别名。

名称 类型 描述
type String

A string representing the event type being removed.

callback Function

The callback to remove.

target Object

The target 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 touchEnd eventListener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);

targetOff
(
  • target
)

删除指定目标上的所有注册回调。

名称 类型 描述
target Object

The target to be searched for all related callbacks

once
(
  • type
  • callback
  • target
  • useCapture
)

注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。

名称 类型 描述
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).

target Object

The target 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
(
  • event
)

分发事件到事件流中。

名称 类型 描述
event Event

The Event object that is dispatched into the event flow

emit
(
  • message
  • [detail ]
)

该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。

名称 类型 描述
message String

the message to send

detail optional

whatever argument the message needs

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index