Cocos Creator API

1.3.0

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

Node

: 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

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 activeInHierarchy if you want to check if the Node is actually treated as active in the scene.

:

node.active = false;

activeInHierarchy Boolean

Indicates whether this node is active in the scene.

:

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

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

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.

name String

Name of node.

:

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

parent Node

The parent of the node.

:

node.parent = newNode;

uuid String readOnly

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

:

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

skewX Number

Skew x

:

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

skewY Number

Skew y

:

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

zIndex Number

Z order in depth which stands for the drawing order.

:

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

rotation Number

Rotation of node.

:

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

rotationX Number

Rotation on x axis.

:

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

rotationY Number

Rotation on y axis.

:

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

scaleX Number

Scale on x axis.

:

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

scaleY Number

Scale on y axis.

:

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

x Number

x axis position of node.

:

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

y Number

y axis position of node.

:

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

children Node[] readOnly

All children nodes.

:

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

childrenCount Number readOnly

All children nodes.

:

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

anchorX Number

Anchor point's position on x axis.

:

node.anchorX = 0;

anchorY Number

Anchor point's position on y axis.

:

node.anchorY = 0;

width Number

Width of node.

:

node.width = 100;

height Number

Height of node.

:

node.height = 100;

_ignoreAnchor Boolean private

Indicate whether ignore the anchor point property for positioning.

tag Number

Tag of node.

:

node.tag = 1001;

opacity Number

Opacity of node, default value is 255.

:

node.opacity = 255;

cascadeOpacity Boolean

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

:

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

color Color

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

:

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

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

__eventTargets EventTarget[] private

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

scale Number

The local scale relative to the parent.

:

node.scale = 1;

_name String private

_objFlags Number private

isValid Boolean readOnly

Indicates whether the object is not yet destroyed.

:

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

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.

typeOrClassName Function | String

:

:

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

typeOrClassName Function | String

:

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.

typeOrClassName Function | String

:

:

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.

typeOrClassName Function | String

:

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.

typeOrClassName Function | String

The constructor or the class name of the component to add

:

: Component

The newly added component

:

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

_addComponentAt
(
  • comp
  • index
)
private

This api should only used by undo system

comp Component
index Number

removeComponent
(
  • component
)
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.

component String | Function | Component

The need remove component.

:

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

_getDependComponent
(
  • depended
)
Component private

depended Component

:

on
(
  • type
  • callback
  • [target ]
  • useCapture
)
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.

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

:

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
)

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

:

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.

target Object

The target to be searched for all related callbacks

:

node.targetOff(target);

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.

action Action

:

: Action

An Action pointer

:

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

stopAllActions ( )

Stops and removes all actions from the running action list .

:

node.stopAllActions();

stopAction
(
  • action
)

Stops and removes an action from the running action list.

action Action

An action object to be removed.

:

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.

tag Number

A tag that indicates the action to be removed.

:

node.stopAction(1);

getActionByTag
(
  • tag
)
Action

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

tag Number

:

: Action

The action object with the given tag.

:

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.

:

: Number

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

:

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

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.

attrs Object

Properties to be set to node

:

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

getScale ( ) Number

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

:

: Number

The scale factor

:

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

setScale
(
  • scaleX
  • [scaleY =scale]
)

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.

scaleX Number | Vec2

scaleX or scale

scaleY optional Number scale

:

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

getPosition ( ) Vec2

Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.

:

: Vec2

The position (x,y) of the node in OpenGL coordinates

:

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

setPosition
(
  • newPosOrxValue
  • [yValue ]
)

Changes the position (x,y) of the node in cocos2d coordinates.
The original point (0,0) is at the left-bottom corner of screen.
Usually we use cc.v2(x,y) to compose CCVec2 object.
and Passing two numbers (x,y) is more efficient than passing CCPoint object.

newPosOrxValue Vec2 | Number

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

yValue optional Number

Y coordinate for position

:

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

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.

:

: Vec2

The anchor point of node.

:

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.

point Vec2 | Number

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

y optional Number

The y axis anchor of node.

:

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

getAnchorPointInPoints ( )