Scene Class
Extends Node
Module: cc
cc.Scene is a subclass of cc.Node that is used only as an abstract concept.
cc.Scene and cc.Node are almost identical with the difference that users can not modify cc.Scene manually.
Index
Properties
autoReleaseAssetsBooleanIndicates whether all (directly or indirectly) static referenced assets of this scene are releasable by default after scene unloading.groupIndexIntegerGroup index of node....groupStringGroup of node....positionVec2The position (x, y) of the node in its parent's coordinates.xNumberx axis position of node.yNumbery axis position of node.rotationNumberRotation of node.rotationXNumberRotation on x axis.rotationYNumberRotation on y axis.scaleXNumberScale on x axis.scaleYNumberScale on y axis.skewXNumberSkew xskewYNumberSkew yopacityNumberOpacity of node, default value is 255.cascadeOpacityBooleanIndicate whether node's opacity value affect its child nodes, default value is true.colorColorColor of node, default value is white: (255, 255, 255).anchorXNumberAnchor point's position on x axis.anchorYNumberAnchor point's position on y axis.widthNumberWidth of node.heightNumberHeight of node.zIndexNumberZ order in depth which stands for the drawing order._sgNode_ccsg.NodeCurrent scene graph node for this node._sizeProvider_ccsg.NodeCurrent active size provider for this node.scaleNumberThe local scale relative to the parent._componentsComponent[]_prefabPrefabInfoThe PrefabInfo object_persistNodeBooleanIf true, the node is an persist node which won't be destroyed during scene transition.nameStringName of node.uuidStringThe uuid for editor, will be stripped before building project.childrenNode[]All children nodes.childrenCountNumberAll children nodes.activeBooleanThe local active state of this node....activeInHierarchyBooleanIndicates whether this node is active in the scene.tagNumberTag of node.__eventTargetsEventTarget[]Register all related EventTargets,...parentNodeThe parent of the node._nameString_objFlagsNumberisValidBooleanIndicates whether the object is not yet destroyed.
Methods
constructoronRegister a callback of a specific event type on Node....offRemoves the callback previously registered with the same type, callback, target and or useCapture.targetOffRemoves all callbacks previously registered with the same target.pauseSystemEventsPause node related system events registered with the current Node.resumeSystemEventsResume node related system events registered with the current Node.runActionThe node becomes the action's target.pauseAllActionsPause all actions running on the current node.resumeAllActionsResume all paused actions on the current node.stopAllActionsStops and removes all actions from the running action list .stopActionStops and removes an action from the running action list.stopActionByTagRemoves an action from the running action list by its tag.getActionByTagReturns an action from the running action list by its tag.getNumberOfRunningActionsComposable actions are counted as 1 action.getPositionReturns a copy of the position (x, y) of the node in its parent's coordinates.setPositionSets the position (x, y) of the node in its parent's coordinates....getScaleReturns the scale factor of the node.setScaleSets the scale factor of the node.getContentSizeReturns a copy the untransformed size of the node.setContentSizeAll nodes has a size.setOpacityModifyRGBSet whether color should be changed with the opacity value,...isOpacityModifyRGBGet whether color should be changed with the opacity value.getAnchorPointIt's like a pin in the node where it is "attached" to its parent.setAnchorPointSets the anchor point in percent.getAnchorPointInPointsReturns a copy of the anchor point in absolute pixels.getDisplayedOpacityReturns the displayed opacity of Node,...getDisplayedColorReturns the displayed color of Node,...getNodeToParentTransformARReturns the matrix that transform the node's (local) space coordinates into the parent's space coordinates....getBoundingBoxReturns a "local" axis aligned bounding box of the node.getBoundingBoxToWorldReturns a "world" axis aligned bounding box of the node....getNodeToParentTransformReturns the matrix that transform the node's (local) space coordinates into the parent's space coordinates....getNodeToWorldTransformReturns the world affine transform matrix.getNodeToWorldTransformARReturns the world affine transform matrix.getParentToNodeTransformThe matrix is in Pixels.getWorldToNodeTransformReturns the inverse world affine transform matrix.convertToNodeSpaceConverts a Point to node (local) space coordinates.convertToWorldSpaceConverts a Point to world space coordinates.convertToNodeSpaceARConverts a Point to node (local) space coordinates.convertToWorldSpaceARConverts a local Point to world space coordinates.The result is in Points....convertTouchToNodeSpaceconvenience methods which take a cc.Touch instead of cc.Vec2.convertTouchToNodeSpaceARconverts a cc.Touch (world coordinates) into a local coordinate.addChildAdds a child to the node with z order and tag.cleanupStops all running actions and schedulers.sortAllChildrenSorts the children array depends on children's zIndex and arrivalOrder,...getPositionXReturns the x axis position of the node in cocos2d coordinates.setPositionXSets the x axis position of the node in cocos2d coordinates.getPositionYReturns the y axis position of the node in cocos2d coordinates.setPositionYSets the y axis position of the node in cocos2d coordinates.getLocalZOrderReturns the local Z order of this node.setLocalZOrderLocalZOrder is the 'key' used to sort the node relative to its siblings.isCascadeOpacityEnabledReturns whether node's opacity value affect its child nodes.setCascadeOpacityEnabledEnable or disable cascade opacity, if cascade enabled, child nodes' opacity will be the multiplication of parent opacity and its own opacity.attrProperties configuration function </br>...getChildByTagReturns a child from the container given its tag.getChildByUuidReturns a child from the container given its uuid.getChildByNameReturns a child from the container given its name.insertChildInserts a child to the node at a specified index.getSiblingIndexGet the sibling index.setSiblingIndexSet the sibling index of this node.removeFromParentRemove itself from its parent node.removeChildRemoves a child from the container.removeChildByTagRemoves a child from the container by tag value.removeAllChildrenRemoves all children from the container and do a cleanup all running actions depending on the cleanup parameter.isChildOfIs this node a child of the given node?getComponentReturns the component of supplied type if the node has one attached, null if it doesn't....getComponentsReturns all components of supplied type in the node.getComponentInChildrenReturns the component of supplied type in any of its children using depth first search.getComponentsInChildrenReturns all components of supplied type in self or any of its children.addComponentAdds a component class to the node._addComponentAtThis api should only used by undo systemremoveComponentRemoves a component identified by the given name or removes the component object given._getDependComponentdestroyAllChildrenDestroy all children from the node, and release all their own references to other objects....onceRegister an callback of a specific event type on the EventTarget,...dispatchEventDispatches an event into the event flow.emitSend an event to this object directly, this method will not propagate the event to any other objects.destroyActual object destruction will delayed until before rendering._destructClear all references in the instance._onPreDestroyCalled before the object being destroyed._serializeThe customized serialization for this object._deserializeInit this object from the custom serialized data.
Events
position-changedsize-changedanchor-changedchild-addedchild-removedchild-reordergroup-changedtouchstartactive-in-hierarchy-changedNote: This event is only emitted from the top most node whose active value did changed,...
Details
Properties
autoReleaseAssets
Indicates whether all (directly or indirectly) static referenced assets of this scene are releasable by default after scene unloading.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/CCScene.js:47 |
groupIndex
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.
| meta | description |
|---|---|
| Type | Integer |
| Defined in | cocos2d/core/CCNode.js:400 |
group
Group of node.
Which Group this node belongs to will resolve that this node's collision components can collide with which other collision componentns.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCNode.js:416 |
position
The position (x, y) of the node in its parent's coordinates.
| meta | description |
|---|---|
| Type | Vec2 |
| Defined in | cocos2d/core/CCNode.js:439 |
Examples
cc.log("Node Position: " + node.position);
x
x axis position of node.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:447 |
Examples
node.x = 100;
cc.log("Node Position X: " + node.x);
y
y axis position of node.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:490 |
Examples
node.y = 100;
cc.log("Node Position Y: " + node.y);
rotation
Rotation of node.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:533 |
Examples
node.rotation = 90;
cc.log("Node Rotation: " + node.rotation);
rotationX
Rotation on x axis.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:561 |
Examples
node.rotationX = 45;
cc.log("Node Rotation X: " + node.rotationX);
rotationY
Rotation on y axis.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:587 |
Examples
node.rotationY = 45;
cc.log("Node Rotation Y: " + node.rotationY);
scaleX
Scale on x axis.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:613 |
Examples
node.scaleX = 0.5;
cc.log("Node Scale X: " + node.scaleX);
scaleY
Scale on y axis.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:639 |
Examples
node.scaleY = 0.5;
cc.log("Node Scale Y: " + node.scaleY);
skewX
Skew x
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:665 |
Examples
node.skewX = 0;
cc.log("Node SkewX: " + node.skewX);
skewY
Skew y
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:684 |
Examples
node.skewY = 0;
cc.log("Node SkewY: " + node.skewY);
opacity
Opacity of node, default value is 255.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:703 |
Examples
node.opacity = 255;
cascadeOpacity
Indicate whether node's opacity value affect its child nodes, default value is true.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/CCNode.js:730 |
Examples
cc.log("CascadeOpacity: " + node.cascadeOpacity);
color
Color of node, default value is white: (255, 255, 255).
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/CCNode.js:756 |
Examples
node.color = new cc.Color(255, 255, 255);
anchorX
Anchor point's position on x axis.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:781 |
Examples
node.anchorX = 0;
anchorY
Anchor point's position on y axis.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:806 |
Examples
node.anchorY = 0;
width
Width of node.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:831 |
Examples
node.width = 100;
height
Height of node.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:870 |
Examples
node.height = 100;
zIndex
Z order in depth which stands for the drawing order.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:909 |
Examples
node.zIndex = 1;
cc.log("Node zIndex: " + node.zIndex);
_sgNode
Current scene graph node for this node.
| meta | description |
|---|---|
| Type | _ccsg.Node |
| Defined in | cocos2d/core/CCNode.js:943 |
_sizeProvider
Current active size provider for this node. Size provider can equals to this._sgNode.
| meta | description |
|---|---|
| Type | _ccsg.Node |
| Defined in | cocos2d/core/CCNode.js:965 |
scale
The local scale relative to the parent.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCNode.js:2631 |
Examples
node.scale = 1;
_components
| meta | description |
|---|---|
| Type | Component[] |
| Defined in | cocos2d/core/utils/base-node.js:156 |
_prefab
The PrefabInfo object
| meta | description |
|---|---|
| Type | PrefabInfo |
| Defined in | cocos2d/core/utils/base-node.js:165 |
_persistNode
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.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/utils/base-node.js:173 |
name
Name of node.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/utils/base-node.js:197 |
Examples
node.name = "New Node";
cc.log("Node Name: " + node.name);
uuid
The uuid for editor, will be stripped before building project.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/utils/base-node.js:224 |
Examples
cc.log("Node Uuid: " + node.uuid);
children
All children nodes.
| meta | description |
|---|---|
| Type | Node[] |
| Defined in | cocos2d/core/utils/base-node.js:243 |
Examples
var children = node.children;
for (var i = 0; i < children.length; ++i) {
cc.log("Node: " + children[i]);
}
childrenCount
All children nodes.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/utils/base-node.js:261 |
Examples
var count = node.childrenCount;
cc.log("Node Children Count: " + count);
active
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.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/utils/base-node.js:277 |
Examples
node.active = false;
activeInHierarchy
Indicates whether this node is active in the scene.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/utils/base-node.js:311 |
Examples
cc.log("activeInHierarchy: " + node.activeInHierarchy);
tag
Tag of node.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/utils/base-node.js:325 |
Examples
node.tag = 1001;
__eventTargets
Register all related EventTargets, all event callbacks will be removed in _onPreDestroy
| meta | description |
|---|---|
| Type | EventTarget[] |
| Defined in | cocos2d/core/utils/base-node.js:347 |
parent
The parent of the node.
| meta | description |
|---|---|
| Type | Node |
| Defined in | cocos2d/core/utils/base-node.js:365 |
Examples
node.parent = newNode;
_name
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/platform/CCObject.js:76 |
_objFlags
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/platform/CCObject.js:83 |
isValid
Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
When an object'sdestroyis called, it is actually destroyed after the end of this frame. SoisValidwill return false from the next frame, whileisValidin the current frame will still be true. If you want to determine whether the current frame has calleddestroy, usecc.isValid(obj, true), but this is often caused by a particular logical requirements, which is not normally required.
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/platform/CCObject.js:261 |
Examples
var node = new cc.Node();
cc.log(node.isValid); // true
node.destroy();
cc.log(node.isValid); // true, still valid in this frame
// after a frame...
cc.log(node.isValid); // false, destroyed in the end of last frame
Methods
constructor
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:937 |
Parameters
nameString
on
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.
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/CCNode.js:1182 |
Parameters
typeString A string representing the event type to listen for.See <a href="../classes/Node.html#event_position-changed" class="crosslink">Node Events</a> for all builtin events.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).eventEvent event
targetObject The target (this object) to invoke the callback, can be nulluseCaptureBoolean When set to true, the capture argument prevents callbackfrom 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
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
Removes the callback previously registered with the same type, callback, target and or useCapture. This method is merely an alias to removeEventListener.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1261 |
Parameters
typeString A string representing the event type being removed.callbackFunction The callback to remove.targetObject The target (this object) to invoke the callback, if it's not given, only callback without target will be removeduseCaptureBoolean 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
Removes all callbacks previously registered with the same target.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1290 |
Parameters
targetObject The target to be searched for all related callbacks
Examples
node.targetOff(target);
pauseSystemEvents
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/
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1305 |
Parameters
recursiveBoolean Whether to pause node system events on the sub node tree.
Examples
node.pauseSystemEvents(true);
resumeSystemEvents
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/
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1321 |
Parameters
recursiveBoolean Whether to resume node system events on the sub node tree.
Examples
node.resumeSystemEvents(true);
runAction
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.
| meta | description |
|---|---|
| Returns | Action |
| Defined in | cocos2d/core/CCNode.js:1459 |
Parameters
actionAction
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).
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1491 |
Examples
node.pauseAllActions();
resumeAllActions
Resume all paused actions on the current node. Equals to cc.director.getActionManager().resumeTarget(node).
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1502 |
Examples
node.resumeAllActions();
stopAllActions
Stops and removes all actions from the running action list .
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1513 |
Examples
node.stopAllActions();
stopAction
Stops and removes an action from the running action list.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1524 |
Parameters
actionAction An action object to be removed.
Examples
var action = cc.scaleTo(0.2, 1, 0.6);
node.stopAction(action);
stopActionByTag
Removes an action from the running action list by its tag.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1537 |
Parameters
tagNumber A tag that indicates the action to be removed.
Examples
node.stopAction(1);
getActionByTag
Returns an action from the running action list by its tag.
| meta | description |
|---|---|
| Returns | Action |
| Defined in | cocos2d/core/CCNode.js:1553 |
Parameters
tagNumber
Examples
var action = node.getActionByTag(1);
getNumberOfRunningActions
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.</p>
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCNode.js:1573 |
Examples
var count = node.getNumberOfRunningActions();
cc.log("Running Action Count: " + count);
getPosition
Returns a copy of the position (x, y) of the node in its parent's coordinates.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:1602 |
Examples
cc.log("Node Position: " + node.getPosition());
setPosition
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1614 |
Parameters
newPosOrXVec2 | Number X coordinate for position or the position (x, y) of the node in coordinatesyNumber Y coordinate for position
Examples
node.setPosition(cc.v2(0, 0));
node.setPosition(0, 0);
getScale
Returns the scale factor of the node. Assertion will fail when _scaleX != _scaleY.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCNode.js:1676 |
Examples
cc.log("Node Scale: " + node.getScale());
setScale
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1692 |
Parameters
Examples
node.setScale(cc.v2(1, 1));
node.setScale(1, 1);
getContentSize
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.
| meta | description |
|---|---|
| Returns | Size |
| Defined in | cocos2d/core/CCNode.js:1722 |
Parameters
ignoreSizeProviderBoolean true if you need to get the original size of the node
Examples
cc.log("Content Size: " + node.getContentSize());
setContentSize
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1745 |
Parameters
sizeSize | Number The untransformed size of the node or The untransformed size's width of the node.heightNumber The untransformed size's height of the node.
Examples
node.setContentSize(cc.size(100, 100));
node.setContentSize(100, 100);
setOpacityModifyRGB
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1789 |
Parameters
opacityValueBoolean
Examples
node.setOpacityModifyRGB(true);
isOpacityModifyRGB
Get whether color should be changed with the opacity value.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCNode.js:1810 |
Examples
var hasChange = node.isOpacityModifyRGB();
getAnchorPoint
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.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:1872 |
Examples
cc.log("Node AnchorPoint: " + node.getAnchorPoint());
setAnchorPoint
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:1896 |
Parameters
pointVec2 | Number The anchor point of node or The x axis anchor of node.yNumber The y axis anchor of node.
Examples
node.setAnchorPoint(cc.v2(1, 1));
node.setAnchorPoint(1, 1);
getAnchorPointInPoints
Returns a copy of the anchor point in absolute pixels.
you can only read it. If you wish to modify it, use setAnchorPoint.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:1938 |
Examples
cc.log("AnchorPointInPoints: " + node.getAnchorPointInPoints());
getDisplayedOpacity
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.
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/CCNode.js:1955 |
Examples
var displayOpacity = node.getDisplayedOpacity();
getDisplayedColor
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.
| meta | description |
|---|---|
| Returns | Color |
| Defined in | cocos2d/core/CCNode.js:1985 |
Examples
var displayColor = node.getDisplayedColor();
getNodeToParentTransformAR
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).
| meta | description |
|---|---|
| Returns | AffineTransform |
| Defined in | cocos2d/core/CCNode.js:2003 |
Examples
var affineTransform = node.getNodeToParentTransformAR();
getBoundingBox
Returns a "local" axis aligned bounding box of the node.
The returned box is relative only to its parent.
| meta | description |
|---|---|
| Returns | Rect |
| Defined in | cocos2d/core/CCNode.js:2030 |
Examples
var boundingBox = node.getBoundingBox();
getBoundingBoxToWorld
Returns a "world" axis aligned bounding box of the node.
The bounding box contains self and active children's world bounding box.
| meta | description |
|---|---|
| Returns | Rect |
| Defined in | cocos2d/core/CCNode.js:2046 |
Examples
var newRect = node.getBoundingBoxToWorld();
getNodeToParentTransform
Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
The matrix is in Pixels.
| meta | description |
|---|---|
| Returns | AffineTransform |
| Defined in | cocos2d/core/CCNode.js:2091 |
Examples
var affineTransform = node.getNodeToParentTransform();
getNodeToWorldTransform
Returns the world affine transform matrix. The matrix is in Pixels.
| meta | description |
|---|---|
| Returns | AffineTransform |
| Defined in | cocos2d/core/CCNode.js:2114 |
Examples
var affineTransform = node.getNodeToWorldTransform();
getNodeToWorldTransformAR
Returns the world affine transform matrix. The matrix is in Pixels.
This method is AR (Anchor Relative).
| meta | description |
|---|---|
| Returns | AffineTransform |
| Defined in | cocos2d/core/CCNode.js:2143 |
Examples
var mat = node.getNodeToWorldTransformAR();
getParentToNodeTransform
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.
| meta | description |
|---|---|
| Returns | AffineTransform |
| Defined in | cocos2d/core/CCNode.js:2174 |
Examples
var affineTransform = node.getParentToNodeTransform();
getWorldToNodeTransform
Returns the inverse world affine transform matrix. The matrix is in Pixels. 返回世界坐标系到节点坐标系的逆矩阵。
| meta | description |
|---|---|
| Returns | AffineTransform |
| Defined in | cocos2d/core/CCNode.js:2190 |
Examples
var affineTransform = node.getWorldToNodeTransform();
convertToNodeSpace
Converts a Point to node (local) space coordinates. The result is in Vec2.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:2220 |
Parameters
worldPointVec2
Examples
var newVec2 = node.convertToNodeSpace(cc.v2(100, 100));
convertToWorldSpace
Converts a Point to world space coordinates. The result is in Points.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:2238 |
Parameters
nodePointVec2
Examples
var newVec2 = node.convertToWorldSpace(cc.v2(100, 100));
convertToNodeSpaceAR
Converts a Point to node (local) space coordinates. The result is in Points.
treating the returned/received node point as anchor relative.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:2257 |
Parameters
worldPointVec2
Examples
var newVec2 = node.convertToNodeSpaceAR(cc.v2(100, 100));
convertToWorldSpaceAR
Converts a local Point to world space coordinates.The result is in Points.
treating the returned/received node point as anchor relative.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:2284 |
Parameters
nodePointVec2
Examples
var newVec2 = node.convertToWorldSpaceAR(cc.v2(100, 100));
convertTouchToNodeSpace
convenience methods which take a cc.Touch instead of cc.Vec2.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:2311 |
Parameters
touchTouch The touch object
Examples
var newVec2 = node.convertTouchToNodeSpace(touch);
convertTouchToNodeSpaceAR
converts a cc.Touch (world coordinates) into a local coordinate. This method is AR (Anchor Relative).
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCNode.js:2324 |
Parameters
touchTouch The touch object
Examples
var newVec2 = node.convertTouchToNodeSpaceAR(touch);
addChild
Adds a child to the node with z order and tag.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:2341 |
Parameters
childNode A child nodelocalZOrderNumber Z order for drawing priority. Please refer to setZOrder(int)tagNumber | 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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:2383 |
Examples
node.cleanup();
sortAllChildren
Sorts the children array depends on children's zIndex and arrivalOrder, normally you won't need to invoke this function.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:2405 |
getPositionX
Returns the x axis position of the node in cocos2d coordinates.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCNode.js:2640 |
Examples
var posX = node.getPositionX();
setPositionX
Sets the x axis position of the node in cocos2d coordinates.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:2649 |
Parameters
xNumber
Examples
node.setPositionX(1);
getPositionY
Returns the y axis position of the node in cocos2d coordinates.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCNode.js:2658 |
Examples
var posY = node.getPositionY();
setPositionY
Sets the y axis position of the node in cocos2d coordinates.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:2667 |
Parameters
yNumber The new position in y axis
Examples
node.setPositionY(100);
getLocalZOrder
Returns the local Z order of this node.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCNode.js:2676 |
Examples
var localZorder = node.getLocalZOrder();
setLocalZOrder
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:2685 |
Parameters
localZOrderNumber
Examples
node.setLocalZOrder(1);
isCascadeOpacityEnabled
Returns whether node's opacity value affect its child nodes.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCNode.js:2708 |
Examples
cc.log(node.isCascadeOpacityEnabled());
setCascadeOpacityEnabled
Enable or disable cascade opacity, if cascade enabled, child nodes' opacity will be the multiplication of parent opacity and its own opacity.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCNode.js:2717 |
Parameters
cascadeOpacityEnabledBoolean
Examples
node.setCascadeOpacityEnabled(true);
attr
Properties configuration function </br> All properties in attrs will be set to the node, </br> when the setter of the node is available, </br> the property will be set via setter function.</br>
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:436 |
Parameters
attrsObject Properties to be set to node
Examples
var attrs = { key: 0, num: 100 };
node.attr(attrs);
getChildByTag
Returns a child from the container given its tag.
| meta | description |
|---|---|
| Returns | Node |
| Defined in | cocos2d/core/utils/base-node.js:455 |
Parameters
aTagNumber An identifier to find the child node.
Examples
var child = node.getChildByTag(1001);
getChildByUuid
Returns a child from the container given its uuid.
| meta | description |
|---|---|
| Returns | Node |
| Defined in | cocos2d/core/utils/base-node.js:476 |
Parameters
uuidString The uuid to find the child node.
Examples
var child = node.getChildByUuid(uuid);
getChildByName
Returns a child from the container given its name.
| meta | description |
|---|---|
| Returns | Node |
| Defined in | cocos2d/core/utils/base-node.js:499 |
Parameters
nameString A name to find the child node.
Examples
var child = node.getChildByName("Test Node");
insertChild
Inserts a child to the node at a specified index.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:537 |
Parameters
childNode the child node to be insertedsiblingIndexNumber the sibling index to place the child in
Examples
node.insertChild(child, 2);
getSiblingIndex
Get the sibling index.
| meta | description |
|---|---|
| Returns | number |
| Defined in | cocos2d/core/utils/base-node.js:555 |
Examples
var index = node.getSiblingIndex();
setSiblingIndex
Set the sibling index of this node.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:572 |
Parameters
indexNumber
Examples
node.setSiblingIndex(1);
removeFromParent
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:607 |
Parameters
cleanupBoolean true if all actions and callbacks on this node should be removed, false otherwise.
Examples
node.removeFromParent();
node.removeFromParent(false);
removeChild
Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter. </p>
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:631 |
Parameters
childNode The child node which will be removed.cleanupBoolean 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
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:659 |
Parameters
tagNumber An integer number that identifies a child nodecleanupBoolean 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
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:685 |
Parameters
cleanupBoolean true if all running actions on all children nodes should be cleanup, false otherwise.
Examples
node.removeAllChildren();
node.removeAllChildren(false);
isChildOf
Is this node a child of the given node?
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/utils/base-node.js:721 |
Parameters
parentNode
Examples
node.isChildOf(newNode);
getComponent
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.
| meta | description |
|---|---|
| Returns | Component |
| Defined in | cocos2d/core/utils/base-node.js:744 |
Parameters
Examples
// get sprite component.
var sprite = node.getComponent(cc.Sprite);
// get custom test calss.
var test = node.getComponent("Test");
getComponents
Returns all components of supplied type in the node.
| meta | description |
|---|---|
| Returns | Component[] |
| Defined in | cocos2d/core/utils/base-node.js:771 |
Parameters
Examples
var sprites = node.getComponents(cc.Sprite);
var tests = node.getComponents("Test");
getComponentInChildren
Returns the component of supplied type in any of its children using depth first search.
| meta | description |
|---|---|
| Returns | Component |
| Defined in | cocos2d/core/utils/base-node.js:792 |
Parameters
Examples
var sprite = node.getComponentInChildren(cc.Sprite);
var Test = node.getComponentInChildren("Test");
getComponentsInChildren
Returns all components of supplied type in self or any of its children.
| meta | description |
|---|---|
| Returns | Component[] |
| Defined in | cocos2d/core/utils/base-node.js:813 |
Parameters
Examples
var sprites = node.getComponentsInChildren(cc.Sprite);
var tests = node.getComponentsInChildren("Test");
addComponent
Adds a component class to the node. You can also add component to node by passing in the name of the script.
| meta | description |
|---|---|
| Returns | Component |
| Defined in | cocos2d/core/utils/base-node.js:849 |
Parameters
Examples
var sprite = node.addComponent(cc.Sprite);
var test = node.addComponent("Test");
_addComponentAt
This api should only used by undo system
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:937 |
Parameters
removeComponent
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:983 |
| Deprecated | please destroy the component to remove it. |
Parameters
Examples
node.removeComponent(cc.Sprite);
var Test = require("Test");
node.removeComponent(Test);
_getDependComponent
| meta | description |
|---|---|
| Returns | Component |
| Defined in | cocos2d/core/utils/base-node.js:1011 |
Parameters
dependedComponent
destroyAllChildren
Destroy all children from the node, and release all their own references to other objects.
Actual destruct operation will delayed until before rendering.
| meta | description |
|---|---|
| Defined in | cocos2d/core/utils/base-node.js:1075 |
Examples
node.destroyAllChildren();
once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:352 |
Parameters
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).eventEvent event
targetObject The target (this object) to invoke the callback, can be nulluseCaptureBoolean When set to true, the capture argument prevents callbackfrom 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
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:397 |
Parameters
eventEvent The Event object that is dispatched into the event flow
emit
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:411 |
Parameters
messageString the message to senddetailAny whatever argument the message needs
destroy
Destroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
From the next frame, this object is not usable any more.
You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/platform/CCObject.js:296 |
Examples
obj.destroy();
_destruct
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; } } }
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:428 |
_onPreDestroy
Called before the object being destroyed.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:461 |
_serialize
The customized serialization for this object. (Editor Only)
| meta | description |
|---|---|
| Returns | object |
| Defined in | cocos2d/core/platform/CCObject.js:486 |
Parameters
exportingBoolean
_deserialize
Init this object from the custom serialized data.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/CCObject.js:496 |
Parameters
dataObject the serialized json datactx_Deserializer
Events
position-changed Event
Module: cc
Index
Details
size-changed Event
Module: cc
Index
Details
anchor-changed Event
Module: cc
Index
Details
child-added Event
Module: cc
Index
Details
child-removed Event
Module: cc
Index
Details
child-reorder Event
Module: cc
Index
Details
group-changed Event
Module: cc
Index
Details
touchstart Event
Module: cc
Index
Details
active-in-hierarchy-changed Event
Note: This event is only emitted from the top most node whose active value did changed, not including its child nodes.