Cocos Creator API

1.3.0

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

EventTarget

: 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

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

Register an callback of a specific event type on the EventTarget. This method is merely an alias to 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
)

Removes the callback previously registered with the same type, callback, target and or useCapture. This method is merely an alias to 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
)

Removes all callbacks previously registered with the same target.

target Object

The target to be searched for all related callbacks

once
(
  • type
  • callback
  • target
  • useCapture
)

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

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
)

Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.

event Event

The Event object that is dispatched into the event flow

emit
(
  • message
  • [detail ]
)

Send an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.

message String

the message to send

detail optional Any

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