Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

Node

Module: cc

Class of all entities in Cocos Creator scenes.
Node also inherits from Event Target, it permits Node to dispatch events. For events supported by Node, please refer to Node.EventType

Properties

groupIndex Integer

Group index of node.
Which Group this node belongs to will resolve that this node's collision components can collide with which other collision componentns.

group String

Group of node.
Which Group this node belongs to will resolve that this node's collision components can collide with which other collision componentns.

position Vec2

The position (x, y) of the node in its parent's coordinates.

examples:

cc.log("Node Position: " + node.position);

x Number

x axis position of node.

examples:

node.x = 100;
cc.log("Node Position X: " + node.x);

y Number

y axis position of node.

examples:

node.y = 100;
cc.log("Node Position Y: " + node.y);

rotation Number

Rotation of node.

examples:

node.rotation = 90;
cc.log("Node Rotation: " + node.rotation);

rotationX Number

Rotation on x axis.

examples:

node.rotationX = 45;
cc.log("Node Rotation X: " + node.rotationX);

rotationY Number

Rotation on y axis.

examples:

node.rotationY = 45;
cc.log("Node Rotation Y: " + node.rotationY);

scaleX Number

Scale on x axis.

examples:

node.scaleX = 0.5;
cc.log("Node Scale X: " + node.scaleX);

scaleY Number

Scale on y axis.

examples:

node.scaleY = 0.5;
cc.log("Node Scale Y: " + node.scaleY);

skewX Number

Skew x

examples:

node.skewX = 0;
cc.log("Node SkewX: " + node.skewX);

skewY Number

Skew y

examples:

node.skewY = 0;
cc.log("Node SkewY: " + node.skewY);

opacity Number

Opacity of node, default value is 255.

examples:

node.opacity = 255;

cascadeOpacity Boolean

Indicate whether node's opacity value affect its child nodes, default value is true.

examples:

cc.log("CascadeOpacity: " + node.cascadeOpacity);

color Color

Color of node, default value is white: (255, 255, 255).

examples:

node.color = new cc.Color(255, 255, 255);

anchorX Number

Anchor point's position on x axis.

examples:

node.anchorX = 0;

anchorY Number

Anchor point's position on y axis.

examples:

node.anchorY = 0;

width Number

Width of node.

examples:

node.width = 100;

height Number

Height of node.

examples:

node.height = 100;

_ignoreAnchor Boolean private

Indicate whether ignore the anchor point property for positioning.

zIndex Number

Z order in depth which stands for the drawing order.

examples:

node.zIndex = 1;
cc.log("Node zIndex: " + node.zIndex);

_sgNode _ccsg.Node private

Current scene graph node for this node.

_sizeProvider _ccsg.Node private

Current active size provider for this node. Size provider can equals to this._sgNode.

scale Number

The local scale relative to the parent.

examples:

node.scale = 1;

_components Component[] private readOnly

_prefab PrefabInfo private

The PrefabInfo object

_persistNode Boolean private

If true, the node is an persist node which won't be destroyed during scene transition. If false, the node will be destroyed automatically when loading a new scene. Default is false.

name String

Name of node.

examples:

node.name = "New Node";
cc.log("Node Name: " + node.name);

uuid String readOnly

The uuid for editor, will be stripped before building project.

examples:

cc.log("Node Uuid: " + node.uuid);

children Node[] readOnly

All children nodes.

examples:

var children = node.children;
for (var i = 0; i < children.length; ++i) {
    cc.log("Node: " + children[i]);
}

childrenCount Number readOnly

All children nodes.

examples:

var count = node.childrenCount;
cc.log("Node Children Count: " + count);

active Boolean

The local active state of this node.
Note that a Node may be inactive because a parent is not active, even if this returns true.
Use Node/activeInHierarchy:property if you want to check if the Node is actually treated as active in the scene.

examples:

node.active = false;

activeInHierarchy Boolean

Indicates whether this node is active in the scene.

examples:

cc.log("activeInHierarchy: " + node.activeInHierarchy);

tag Number

Tag of node.

examples:

node.tag = 1001;

__eventTargets EventTarget[] private

Register all related EventTargets, all event callbacks will be removed in _onPreDestroy

parent Node

The parent of the node.

examples:

node.parent = newNode;

_name String private

_objFlags Number private

isValid Boolean readOnly

Indicates whether the object is not yet destroyed.

examples:

cc.log(obj.isValid);

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

Methods

constructor
(
  • [name ]
)

name type description
name optional String

on
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)
Function

Register a callback of a specific event type on Node.
Use this method to register touch or mouse event permit propagation based on scene graph, you can propagate the event to the parents or swallow it by calling stopPropagation on the event.
It's the recommended way to register touch/mouse event for Node, please do not use cc.eventManager directly for Node.

name type description
type String

A string representing the event type to listen for.
See Node Events for all builtin events.

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 optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

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.

returns:

type: Function

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

examples:

this.node.on(cc.Node.EventType.TOUCH_START, this.memberFunction, this);  // if "this" is component and the "memberFunction" declared in CCClass.
node.on(cc.Node.EventType.TOUCH_START, callback, this.node);
node.on(cc.Node.EventType.TOUCH_MOVE, callback, this.node);
node.on(cc.Node.EventType.TOUCH_END, callback, this.node);
node.on(cc.Node.EventType.TOUCH_CANCEL, callback, this.node);
node.on("anchor-changed", callback, this);

off
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Removes the callback previously registered with the same type, callback, target and or useCapture. This method is merely an alias to removeEventListener.

name type description
type String

A string representing the event type being removed.

callback Function

The callback to remove.

target optional Object

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

useCapture optional Boolean false

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.

examples:

this.node.off(cc.Node.EventType.TOUCH_START, this.memberFunction, this);
node.off(cc.Node.EventType.TOUCH_START, callback, this.node);
node.off("anchor-changed", callback, this);

targetOff
(
  • target
)

Removes all callbacks previously registered with the same target.

name type description
target Object

The target to be searched for all related callbacks

examples:

node.targetOff(target);

pauseSystemEvents
(
  • recursive
)

Pause node related system events registered with the current Node. Node system events includes touch and mouse events. If recursive is set to true, then this API will pause the node system events for the node and all nodes in its sub node tree. Reference: http://cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/internal-events/

name type description
recursive Boolean

Whether to pause node system events on the sub node tree.

examples:

node.pauseSystemEvents(true);

resumeSystemEvents
(
  • recursive
)

Resume node related system events registered with the current Node. Node system events includes touch and mouse events. If recursive is set to true, then this API will resume the node system events for the node and all nodes in its sub node tree. Reference: http://cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/internal-events/

name type description
recursive Boolean

Whether to resume node system events on the sub node tree.

examples:

node.resumeSystemEvents(true);

runAction
(
  • action
)
Action

Executes an action, and returns the action that is executed.
The node becomes the action's target. Refer to cc.Action's getTarget()
Calling runAction while the node is not active won't have any effect.
Note:You shouldn't modify the action after runAction, that won't take any effect.
if you want to modify, when you define action plus.

name type description
action Action

returns:

type: Action

An Action pointer

examples:

var action = cc.scaleTo(0.2, 1, 0.6);
node.runAction(action);
node.runAction(action).repeatForever(); // fail
node.runAction(action.repeatForever()); // right

pauseAllActions ( )

Pause all actions running on the current node. Equals to cc.director.getActionManager().pauseTarget(node).

examples:

node.pauseAllActions();

resumeAllActions ( )

Resume all paused actions on the current node. Equals to cc.director.getActionManager().resumeTarget(node).

examples:

node.resumeAllActions();

stopAllActions ( )

Stops and removes all actions from the running action list .

examples:

node.stopAllActions();

stopAction
(
  • action
)

Stops and removes an action from the running action list.

name type description
action Action

An action object to be removed.

examples:

var action = cc.scaleTo(0.2, 1, 0.6);
node.stopAction(action);

stopActionByTag
(
  • tag
)

Removes an action from the running action list by its tag.

name type description
tag Number

A tag that indicates the action to be removed.

examples:

node.stopAction(1);

getActionByTag
(
  • tag
)
Action

Returns an action from the running action list by its tag.

name type description
tag Number

returns:

type: Action

The action object with the given tag.

examples:

var action = node.getActionByTag(1);

getNumberOfRunningActions ( ) Number

Returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionsToAdd and actions arrays).
Composable actions are counted as 1 action. Example:
If you are running 1 Sequence of 7 actions, it will return 1.
If you are running 7 Sequences of 2 actions, it will return 7.

returns:

type: Number

The number of actions that are running plus the ones that are schedule to run

examples:

var count = node.getNumberOfRunningActions();
cc.log("Running Action Count: " + count);

getPosition ( ) Vec2

Returns a copy of the position (x, y) of the node in its parent's coordinates.

returns:

type: Vec2

The position (x, y) of the node in its parent's coordinates

examples:

cc.log("Node Position: " + node.getPosition());

setPosition
(
  • newPosOrX
  • [y ]
)

Sets the position (x, y) of the node in its parent's coordinates.
Usually we use cc.v2(x, y) to compose cc.Vec2 object.
and Passing two numbers (x, y) is more efficient than passing cc.Vec2 object.

name type description
newPosOrX Vec2 | Number

X coordinate for position or the position (x, y) of the node in coordinates

y optional Number

Y coordinate for position

examples:

node.setPosition(cc.v2(0, 0));
node.setPosition(0, 0);

getScale ( ) Number

Returns the scale factor of the node. Assertion will fail when _scaleX != _scaleY.

returns:

type: Number

The scale factor

examples:

cc.log("Node Scale: " + node.getScale());

setScale
(
  • scaleX
  • [scaleY ]
)

Sets the scale factor of the node. 1.0 is the default scale factor. This function can modify the X and Y scale at the same time.

name type description
scaleX Number | Vec2

scaleX or scale

scaleY optional Number

examples:

node.setScale(cc.v2(1, 1));
node.setScale(1, 1);

getContentSize
(
  • [ignoreSizeProvider =false]
)
Size

Returns a copy the untransformed size of the node.
The contentSize remains the same no matter the node is scaled or rotated.
All nodes has a size. Layer and Scene has the same size of the screen by default.

name type description
ignoreSizeProvider optional Boolean false

true if you need to get the original size of the node

returns:

type: Size

The untransformed size of the node.

examples:

cc.log("Content Size: " + node.getContentSize());

setContentSize
(
  • size
  • [height ]
)

Sets the untransformed size of the node.
The contentSize remains the same no matter the node is scaled or rotated.
All nodes has a size. Layer and Scene has the same size of the screen.

name type description
size Size | Number

The untransformed size of the node or The untransformed size's width of the node.

height optional Number

The untransformed size's height of the node.

examples:

node.setContentSize(cc.size(100, 100));
node.setContentSize(100, 100);

setOpacityModifyRGB
(
  • opacityValue
)

Set whether color should be changed with the opacity value, useless in ccsg.Node, but this function is override in some class to have such behavior.

name type description
opacityValue Boolean

examples:

node.setOpacityModifyRGB(true);

isOpacityModifyRGB ( ) Boolean

Get whether color should be changed with the opacity value.

returns:

type: Boolean

examples:

var hasChange = node.isOpacityModifyRGB();

getAnchorPoint ( ) Vec2

Returns a copy of the anchor point.
Anchor point is the point around which all transformations and positioning manipulations take place.
It's like a pin in the node where it is "attached" to its parent.
The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
But you can use values higher than (1,1) and lower than (0,0) too.
The default anchor point is (0.5,0.5), so it starts at the center of the node.

returns:

type: Vec2

The anchor point of node.

examples:

cc.log("Node AnchorPoint: " + node.getAnchorPoint());

setAnchorPoint
(
  • point
  • [y ]
)

Sets the anchor point in percent.
anchor point is the point around which all transformations and positioning manipulations take place.
It's like a pin in the node where it is "attached" to its parent.
The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
But you can use values higher than (1,1) and lower than (0,0) too.
The default anchor point is (0.5,0.5), so it starts at the center of the node.

name type description
point Vec2 | Number

The anchor point of node or The x axis anchor of node.

y optional Number

The y axis anchor of node.

examples:

node.setAnchorPoint(cc.v2(1, 1));
node.setAnchorPoint(1, 1);

getAnchorPointInPoints ( ) Vec2

Returns a copy of the anchor point in absolute pixels.
you can only read it. If you wish to modify it, use setAnchorPoint.

returns:

type: Vec2

The anchor point in absolute pixels.

examples:

cc.log("AnchorPointInPoints: " + node.getAnchorPointInPoints());

getDisplayedOpacity ( ) number

Returns the displayed opacity of Node, the difference between displayed opacity and opacity is that displayed opacity is calculated based on opacity and parent node's opacity when cascade opacity enabled.

returns:

type: number

displayed opacity

examples:

var displayOpacity = node.getDisplayedOpacity();

getDisplayedColor ( ) Color

Returns the displayed color of Node, the difference between displayed color and color is that displayed color is calculated based on color and parent node's color when cascade color enabled.

returns:

type: Color

examples:

var displayColor = node.getDisplayedColor();

getNodeToParentTransformAR ( ) AffineTransform

Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
The matrix is in Pixels.
This method is AR (Anchor Relative).

returns:

type: AffineTransform

The affine transform object

examples:

var affineTransform = node.getNodeToParentTransformAR();

getBoundingBox ( ) Rect

Returns a "local" axis aligned bounding box of the node.
The returned box is relative only to its parent.

returns:

type: Rect

The calculated bounding box of the node

examples:

var boundingBox = node.getBoundingBox();

getBoundingBoxToWorld ( ) Rect

Returns a "world" axis aligned bounding box of the node.
The bounding box contains self and active children's world bounding box.

returns:

type: Rect

examples:

var newRect = node.getBoundingBoxToWorld();

getNodeToParentTransform ( ) AffineTransform

Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
The matrix is in Pixels.

returns:

type: AffineTransform

The affine transform object

examples:

var affineTransform = node.getNodeToParentTransform();

getNodeToWorldTransform ( ) AffineTransform

Returns the world affine transform matrix. The matrix is in Pixels.

returns:

examples:

var affineTransform = node.getNodeToWorldTransform();

getNodeToWorldTransformAR ( ) AffineTransform

Returns the world affine transform matrix. The matrix is in Pixels.
This method is AR (Anchor Relative).

returns:

examples:

var mat = node.getNodeToWorldTransformAR();

getParentToNodeTransform ( ) AffineTransform

Returns the matrix that transform parent's space coordinates to the node's (local) space coordinates.
The matrix is in Pixels. The returned transform is readonly and cannot be changed.

returns:

examples:

var affineTransform = node.getParentToNodeTransform();

getWorldToNodeTransform ( ) AffineTransform

Returns the inverse world affine transform matrix. The matrix is in Pixels. 返回世界坐标系到节点坐标系的逆矩阵。

returns:

examples:

var affineTransform = node.getWorldToNodeTransform();

convertToNodeSpace
(
  • worldPoint
)
Vec2

Converts a Point to node (local) space coordinates. The result is in Vec2.

name type description
worldPoint Vec2

returns:

type: Vec2

examples:

var newVec2 = node.convertToNodeSpace(cc.v2(100, 100));

convertToWorldSpace
(
  • nodePoint
)
Vec2

Converts a Point to world space coordinates. The result is in Points.

name type description
nodePoint Vec2

returns:

type: Vec2

examples:

var newVec2 = node.convertToWorldSpace(cc.v2(100, 100));

convertToNodeSpaceAR
(
  • worldPoint
)
Vec2

Converts a Point to node (local) space coordinates. The result is in Points.
treating the returned/received node point as anchor relative.

name type description
worldPoint Vec2

returns:

type: Vec2

examples:

var newVec2 = node.convertToNodeSpaceAR(cc.v2(100, 100));

convertToWorldSpaceAR
(
  • nodePoint
)
Vec2

Converts a local Point to world space coordinates.The result is in Points.
treating the returned/received node point as anchor relative.

name type description
nodePoint Vec2

returns:

type: Vec2

examples:

var newVec2 = node.convertToWorldSpaceAR(cc.v2(100, 100));

convertTouchToNodeSpace
(
  • touch
)
Vec2

convenience methods which take a cc.Touch instead of cc.Vec2.

name type description
touch Touch

The touch object

returns:

type: Vec2

examples:

var newVec2 = node.convertTouchToNodeSpace(touch);

convertTouchToNodeSpaceAR
(
  • touch
)
Vec2

converts a cc.Touch (world coordinates) into a local coordinate. This method is AR (Anchor Relative).

name type description
touch Touch

The touch object

returns:

type: Vec2

examples:

var newVec2 = node.convertTouchToNodeSpaceAR(touch);

addChild
(
  • child
  • [localZOrder ]
  • [tag ]
)

"add" logic MUST only be in this method

name type description
child Node

A child node

localZOrder optional Number

Z order for drawing priority. Please refer to setZOrder(int)

tag optional Number | String

An integer or a name to identify the node easily. Please refer to setTag(int) and setName(string)

examples:

node.addChild(newNode, 1, 1001);

cleanup ( )

Stops all running actions and schedulers.

examples:

node.cleanup();

sortAllChildren ( )

Sorts the children array depends on children's zIndex and arrivalOrder, normally you won't need to invoke this function.

getPositionX ( ) Number

Returns the x axis position of the node in cocos2d coordinates.

returns:

type: Number

x - The new position in x axis

examples:

var posX = node.getPositionX();

setPositionX
(
  • x
)

Sets the x axis position of the node in cocos2d coordinates.

name type description
x Number

examples:

node.setPositionX(1);

getPositionY ( ) Number

Returns the y axis position of the node in cocos2d coordinates.

returns:

type: Number

examples:

var posY = node.getPositionY();

setPositionY
(
  • y
)

Sets the y axis position of the node in cocos2d coordinates.

name type description
y Number

The new position in y axis

examples:

node.setPositionY(100);

getLocalZOrder ( ) Number

Returns the local Z order of this node.

returns:

type: Number

The local (relative to its siblings) Z order.

examples:

var localZorder = node.getLocalZOrder();

setLocalZOrder
(
  • localZOrder
)

LocalZOrder is the 'key' used to sort the node relative to its siblings.

The Node's parent will sort all its children based ont the LocalZOrder value.
If two nodes have the same LocalZOrder, then the node that was added first to the children's array
will be in front of the other node in the array.
Also, the Scene Graph is traversed using the "In-Order" tree traversal algorithm ( http://en.wikipedia.org/wiki/Tree_traversal#In-order )
And Nodes that have LocalZOder values smaller than 0 are the "left" subtree
While Nodes with LocalZOder greater than 0 are the "right" subtree.

name type description
localZOrder Number

examples:

node.setLocalZOrder(1);

isCascadeOpacityEnabled ( ) Boolean

Returns whether node's opacity value affect its child nodes.

returns:

type: Boolean

examples:

cc.log(node.isCascadeOpacityEnabled());

setCascadeOpacityEnabled
(
  • cascadeOpacityEnabled
)

Enable or disable cascade opacity, if cascade enabled, child nodes' opacity will be the multiplication of parent opacity and its own opacity.

name type description
cascadeOpacityEnabled Boolean

examples:

node.setCascadeOpacityEnabled(true);

attr
(
  • attrs
)

Properties configuration function
All properties in attrs will be set to the node,
when the setter of the node is available,
the property will be set via setter function.

name type description
attrs Object

Properties to be set to node

examples:

var attrs = { key: 0, num: 100 };
node.attr(attrs);

getChildByTag
(
  • aTag
)
Node

Returns a child from the container given its tag.

name type description
aTag Number

An identifier to find the child node.

returns:

type: Node

a CCNode object whose tag equals to the input parameter

examples:

var child = node.getChildByTag(1001);

getChildByUuid
(
  • uuid
)
Node

Returns a child from the container given its uuid.

name type description
uuid String

The uuid to find the child node.

returns:

type: Node

a Node whose uuid equals to the input parameter

examples:

var child = node.getChildByUuid(uuid);

getChildByName
(
  • name
)
Node

Returns a child from the container given its name.

name type description
name String

A name to find the child node.

returns:

type: Node

a CCNode object whose name equals to the input parameter

examples:

var child = node.getChildByName("Test Node");

getSiblingIndex ( ) number

Get the sibling index.

returns:

type: number

examples:

var index = node.getSiblingIndex();

setSiblingIndex
(
  • index
)

Set the sibling index of this node.

name type description
index Number

examples:

node.setSiblingIndex(1);

removeFromParent
(
  • [cleanup =true]
)

Remove itself from its parent node. If cleanup is true, then also remove all events and actions.
If the cleanup parameter is not passed, it will force a cleanup, so it is recommended that you always pass in the false parameter when calling this API.
If the node orphan, then nothing happens.

name type description
cleanup optional Boolean true

true if all actions and callbacks on this node should be removed, false otherwise.

examples:

node.removeFromParent();
node.removeFromParent(false);

removeChild
(
  • child
  • [cleanup =true]
)

Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.

If the cleanup parameter is not passed, it will force a cleanup.
"remove" logic MUST only be on this method
If a class wants to extend the 'removeChild' behavior it only needs
to override this method.

name type description
child Node

The child node which will be removed.

cleanup optional Boolean true

true if all running actions and callbacks on the child node will be cleanup, false otherwise.

examples:

node.removeChild(newNode);
node.removeChild(newNode, false);

removeChildByTag
(
  • tag
  • [cleanup =true]
)

Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter. If the cleanup parameter is not passed, it will force a cleanup.

name type description
tag Number

An integer number that identifies a child node

cleanup optional Boolean true

true if all running actions and callbacks on the child node will be cleanup, false otherwise.

examples:

node.removeChildByTag(1001);
node.removeChildByTag(1001, false);

removeAllChildren
(
  • [cleanup =true]
)

Removes all children from the container and do a cleanup all running actions depending on the cleanup parameter.
If the cleanup parameter is not passed, it will force a cleanup.

name type description
cleanup optional Boolean true

true if all running actions on all children nodes should be cleanup, false otherwise.

examples:

node.removeAllChildren();
node.removeAllChildren(false);

isChildOf
(
  • parent
)
Boolean

Is this node a child of the given node?

name type description
parent Node

returns:

type: Boolean

Returns true if this node is a child, deep child or identical to the given node.

examples:

node.isChildOf(newNode);

getComponent
(
  • typeOrClassName
)
Component

Returns the component of supplied type if the node has one attached, null if it doesn't.
You can also get component in the node by passing in the name of the script.

name type description
typeOrClassName Function | String

returns:

type: Component

examples:

// get sprite component.
var sprite = node.getComponent(cc.Sprite);
// get custom test calss.
var test = node.getComponent("Test");

getComponents
(
  • typeOrClassName
)
Component[]

Returns all components of supplied type in the node.

name type description
typeOrClassName Function | String

returns:

type: Component[]

examples:

var sprites = node.getComponents(cc.Sprite);
var tests = node.getComponents("Test");

getComponentInChildren
(
  • typeOrClassName
)
Component

Returns the component of supplied type in any of its children using depth first search.

name type description
typeOrClassName Function | String

returns:

type: Component

examples:

var sprite = node.getComponentInChildren(cc.Sprite);
var Test = node.getComponentInChildren("Test");

getComponentsInChildren
(
  • typeOrClassName
)
Component[]

Returns all components of supplied type in self or any of its children.

name type description
typeOrClassName Function | String

returns:

type: Component[]

examples:

var sprites = node.getComponentsInChildren(cc.Sprite);
var tests = node.getComponentsInChildren("Test");

addComponent
(
  • typeOrClassName
)
Component

Adds a component class to the node. You can also add component to node by passing in the name of the script.

name type description
typeOrClassName Function | String

The constructor or the class name of the component to add

returns:

type: Component

The newly added component

examples:

var sprite = node.addComponent(cc.Sprite);
var test = node.addComponent("Test");

_addComponentAt
(
  • comp
  • index
)
private

This api should only used by undo system

name type description
comp Component
index Number

removeComponent
(
  • component
)
deprecated

deprecated: please destroy the component to remove it.

Removes a component identified by the given name or removes the component object given. You can also use component.destroy() if you already have the reference.

name type description
component String | Function | Component

The need remove component.

examples:

node.removeComponent(cc.Sprite);
var Test = require("Test");
node.removeComponent(Test);

_getDependComponent
(
  • depended
)
Component private

name type description
depended Component

returns:

type: Component

destroyAllChildren ( )

Destroy all children from the node, and release all their own references to other objects.
Actual destruct operation will delayed until before rendering.

examples:

node.destroyAllChildren();

once
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Inherited from EventTarget:

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

name type description
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 optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

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.

examples:

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

dispatchEvent
(
  • event
)

Inherited from EventTarget:

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

name type description
event Event

The Event object that is dispatched into the event flow

emit
(
  • message
  • [detail ]
)

Inherited from EventTarget:

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.

name type description
message String

the message to send

detail optional Any

whatever argument the message needs

destroy ( ) Boolean

Destroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
After destroy, this CCObject is not usable any more. You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.

returns:

type: Boolean

whether it is the first time the destroy being called

examples:

obj.destroy();

_destruct ( ) private

Clear all references in the instance.

NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _destruct: function () { for (var key in this) { if (this.hasOwnProperty(key)) { switch (typeof this[key]) { case 'string': this[key] = ''; break; case 'object': case 'function': this[key] = null; break; } } }

_onPreDestroy ( ) private

Called before the object being destroyed.

_serialize
(
  • exporting
)
object private

The customized serialization for this object. (Editor Only)

name type description
exporting Boolean

returns:

type: object

the serialized json data object

_deserialize
(
  • data
  • ctx
)
private

Init this object from the custom serialized data.

name type description
data Object

the serialized json data

ctx _Deserializer

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

Events

position-changed

Event Payload:

size-changed

Event Payload:

anchor-changed

Event Payload:

child-added

Event Payload:

child-removed

Event Payload:

child-reorder

Event Payload:

group-changed

Event Payload:

touchstart

active-in-hierarchy-changed

Note: This event is only emitted from the top most node whose active value did changed, not including its child nodes.

Event Payload:

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