Scene 类型
继承于 Node
模块: cc
cc.Scene 是 cc.Node 的子类,仅作为一个抽象的概念。
cc.Scene 和 cc.Node 有点不同,用户不应直接修改 cc.Scene。
索引
属性(properties)
autoReleaseAssetsBoolean指示该场景中直接或间接静态引用到的所有资源是否默认在场景切换后自动释放。groupIndexInteger节点的分组索引。
节点的分组将关系到节点的碰撞组件可以与哪些碰撞组件相碰撞。groupString节点的分组。
节点的分组将关系到节点的碰撞组件可以与哪些碰撞组件相碰撞。positionVec2节点在父节点坐标系中的位置(x, y)。xNumber节点 X 轴坐标。yNumber节点 Y 轴坐标。rotationNumber该节点旋转角度。rotationXNumber该节点 X 轴旋转角度。rotationYNumber该节点 Y 轴旋转角度。scaleXNumber节点 X 轴缩放。scaleYNumber节点 Y 轴缩放。skewXNumber该节点 Y 轴倾斜角度。skewYNumber该节点 X 轴倾斜角度。opacityNumber节点透明度,默认值为 255。cascadeOpacityBoolean节点的不透明度值是否影响其子节点,默认值为 true。colorColor节点颜色。默认为白色,数值为:(255,255,255)。anchorXNumber节点 X 轴锚点位置。anchorYNumber节点 Y 轴锚点位置。widthNumber节点宽度。heightNumber节点高度。zIndexNumber该节点渲染排序的 Z 轴深度。_sgNode_ccsg.NodeCurrent scene graph node for this node._sizeProvider_ccsg.NodeCurrent active size provider for this node. Size provider can equals to this._sgNode.scaleNumber节点相对父节点的缩放。_componentsComponent[]_prefabPrefabInfoThe PrefabInfo object_persistNodeBooleanIf 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.nameString该节点名称。uuidString主要用于编辑器的 uuid,在编辑器下可用于持久化存储,在项目构建之后将变成自增的 id。childrenNode[]节点的所有子节点。childrenCountNumber节点的子节点数量。activeBoolean当前节点的自身激活状态。
值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。
如果你想检查节点在场景中实际的激活状态可以使用 Node/activeInHierarchy:property。activeInHierarchyBoolean表示此节点是否在场景中激活。tagNumber节点标签。__eventTargetsEventTarget[]Register all related EventTargets, all event callbacks will be removed in _onPreDestroyparentNode该节点的父节点。_nameString_objFlagsNumberisValidBoolean表示该对象是否可用(被销毁后将不可用)。
方法
constructoron在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的 this 对象。
同时您可以将事件派发到父节点或者通过调用 stopPropagation 拦截它。
推荐使用这种方式来监听节点上的触摸或鼠标事件,请不要在节点上直接使用 cc.eventManager。off删除之前与同类型,回调,目标或 useCapture 注册的回调。targetOff移除目标上的所有注册事件。pauseSystemEvents暂停当前节点上注册的所有节点系统事件,节点系统事件包含触摸和鼠标事件。 如果传递 recursive 为 true,那么这个 API 将暂停本节点和它的子树上所有节点的节点系统事件。 参考:http://cocos.com/docs/creator/scripting/internal-events.htmlresumeSystemEvents恢复当前节点上注册的所有节点系统事件,节点系统事件包含触摸和鼠标事件。 如果传递 recursive 为 true,那么这个 API 将恢复本节点和它的子树上所有节点的节点系统事件。 参考:http://cocos.com/docs/creator/scripting/internal-events.htmlrunAction执行并返回该执行的动作。该节点将会变成动作的目标。
调用 runAction 时,节点自身处于不激活状态将不会有任何效果。
注意:你不应该修改 runAction 后的动作,将无法发挥作用,如果想进行修改,请在定义 action 时加入。pauseAllActions暂停本节点上所有正在运行的动作。和cc.director.getActionManager().pauseTarget(node);等价。resumeAllActions恢复运行本节点上所有暂停的动作。和cc.director.getActionManager().resumeTarget(node);等价。stopAllActions停止并且移除所有正在运行的动作列表。stopAction停止并移除指定的动作。stopActionByTag停止并且移除指定标签的动作。getActionByTag通过标签获取指定动作。getNumberOfRunningActions获取运行着的动作加上正在调度运行的动作的总数。
例如:- 如果你正在运行 7 个动作中的 1 个 Sequence,它将返回 1。
- 如果你正在运行 2 个动作中的 7 个 Sequence,它将返回 7。
- 如果你正在运行 7 个动作中的 1 个 Sequence,它将返回 1。
getPosition获取节点在父节点坐标系中的位置(x, y)。setPosition设置节点在父节点坐标系中的位置。
可以通过两种方式设置坐标点:- 传入 2 个数值 x 和 y。
- 传入 cc.v2(x, y) 类型为 cc.Vec2 的对象。
- 传入 2 个数值 x 和 y。
getScale获取节点的缩放。当 X 轴和 Y 轴有相同的缩放数值时。setScale设置节点的缩放比例,默认值为 1.0。这个函数可以在同一时间修改 X 和 Y 缩放。getContentSize获取节点自身大小,不受该节点是否被缩放或者旋转的影响。setContentSize设置节点原始大小,不受该节点是否被缩放或者旋转的影响。setOpacityModifyRGB设置更改透明度时是否修改RGB值,isOpacityModifyRGB更改透明度时是否修改RGB值。getAnchorPoint获取节点锚点,用百分比表示。
锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。setAnchorPoint设置锚点的百分比。
锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。getAnchorPointInPoints返回锚点的绝对像素位置。
你只能读它。如果您要修改它,使用 setAnchorPoint。getDisplayedOpacity获取节点显示透明度, 显示透明度和透明度之间的不同之处在于当启用级连透明度时, 显示透明度是基于自身透明度和父节点透明度计算的。getDisplayedColor获取节点的显示颜色, 显示颜色和颜色之间的不同之处在于当启用级连颜色时, 显示颜色是基于自身颜色和父节点颜色计算的。getNodeToParentTransformAR返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。
这个矩阵以像素为单位。
该方法基于节点坐标。getBoundingBox返回父节坐标系下的轴向对齐的包围盒。getBoundingBoxToWorld返回节点在世界坐标系下的对齐轴向的包围盒(AABB)。
该边框包含自身和已激活的子节点的世界边框。getNodeToParentTransform返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。这个矩阵以像素为单位。getNodeToWorldTransform返回节点到世界坐标系的仿射变换矩阵。矩阵单位是像素。getNodeToWorldTransformAR返回节点到世界坐标仿射变换矩阵。矩阵单位是像素。
该方法基于节点坐标。getParentToNodeTransform返回将父节点的坐标系转换成节点(局部)的空间坐标系的矩阵。
该矩阵以像素为单位。返回的矩阵是只读的,不能更改。getWorldToNodeTransformconvertToNodeSpace将一个点转换到节点 (局部) 坐标系。结果以 Vec2 为单位。convertToWorldSpace将一个点转换到世界空间坐标系。结果以 Vec2 为单位。convertToNodeSpaceAR将一个点转换到节点 (局部) 空间坐标系。结果以 Vec2 为单位。
返回值将基于节点坐标。convertToWorldSpaceAR将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
返回值将基于世界坐标。convertTouchToNodeSpace将触摸点转换成本地坐标系中位置。convertTouchToNodeSpaceAR转换一个 cc.Touch(世界坐标)到一个局部坐标,该方法基于节点坐标。addChild添加子节点,并且可以修改该节点的 局部 Z 顺序和标签。cleanup停止所有正在播放的动作和计时器。sortAllChildren根据子节点的 zIndex 和 arrivalOrder 进行排序,正常情况下开发者不需要手动调用这个函数。getPositionX获取节点 X 轴坐标。setPositionX设置节点 X 轴坐标。getPositionY获取节点 Y 轴坐标。setPositionY设置节点 Y 轴坐标。getLocalZOrder获取节点局部 Z 轴顺序。setLocalZOrderLocalZOrder 是 “key” (关键)来分辨节点和它兄弟节点的相关性。 父节点将会通过 LocalZOrder 的值来分辨所有的子节点。 如果两个节点有同样的 LocalZOrder,那么先加入子节点数组的节点将会显示在后加入的节点的前面。 同样的,场景图使用 “In-Order(按顺序)” 遍历数算法来遍历 ( http://en.wikipedia.org/wiki/Tree_traversal#In-order ) 并且拥有小于 0 的 LocalZOrder 的值的节点是 “ left ” 子树(左子树) 所以拥有大于 0 的 LocalZOrder 的值得节点是 “ right ”子树(右子树)。isCascadeOpacityEnabled返回节点的不透明度值是否影响其子节点。setCascadeOpacityEnabled启用或禁用级连不透明度,如果级连启用,子节点的不透明度将是父不透明度乘上它自己的不透明度。attr属性配置函数。在 attrs 的所有属性将被设置为节点属性。getChildByTag通过标签获取节点的子节点。getChildByUuid通过 uuid 获取节点的子节点。getChildByName通过名称获取节点的子节点。insertChild插入子节点到指定位置getSiblingIndex获取同级索引。setSiblingIndex设置节点同级索引。removeFromParent从父节点中删除该节点。如果不传入 cleanup 参数或者传入true,那么这个节点上所有绑定的事件、action 都会被删除。
因此建议调用这个 API 时总是传入false参数。
如果这个节点是一个孤节点,那么什么都不会发生。removeChild移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
如果 cleanup 参数不传入,默认为 true 表示清理。removeChildByTag通过标签移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
如果 cleanup 参数不传入,默认为 true 表示清理。removeAllChildren移除节点所有的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
如果 cleanup 参数不传入,默认为 true 表示清理。isChildOf是否是指定节点的子节点?getComponent获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
传入参数也可以是脚本的名称。getComponents返回节点上指定类型的所有组件。getComponentInChildren递归查找所有子节点中第一个匹配指定类型的组件。getComponentsInChildren递归查找自身或所有子节点中指定类型的组件addComponent向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。_addComponentAtThis api should only used by undo systemremoveComponent删除节点上的指定组件,传入参数可以是一个组件构造函数或组件名,也可以是已经获得的组件引用。 如果你已经获得组件引用,你也可以直接调用 component.destroy()_getDependComponentdestroyAllChildren销毁所有子节点,并释放所有它们对其它对象的引用。
实际销毁操作会延迟到当前帧渲染前执行。once注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent分发事件到事件流中。emit该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。destroy销毁该对象,并释放所有它对其它对象的引用。
销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 实际销毁操作会延迟到当前帧渲染前执行。_destructClear 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; } } }
_onPreDestroyCalled before the object being destroyed._serializeThe customized serialization for this object. (Editor Only)_deserializeInit this object from the custom serialized data.
事件
position-changedsize-changedanchor-changedchild-addedchild-removedchild-reordergroup-changedtouchstartactive-in-hierarchy-changed注意:此节点激活时,此事件仅从最顶部的节点发出。
Details
属性(properties)
autoReleaseAssets
指示该场景中直接或间接静态引用到的所有资源是否默认在场景切换后自动释放。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCScene.js:46 |
groupIndex
节点的分组索引。
节点的分组将关系到节点的碰撞组件可以与哪些碰撞组件相碰撞。
| meta | description |
|---|---|
| 类型 | Integer |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:381 |
group
节点的分组。
节点的分组将关系到节点的碰撞组件可以与哪些碰撞组件相碰撞。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:397 |
position
节点在父节点坐标系中的位置(x, y)。
| meta | description |
|---|---|
| 类型 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:420 |
示例
cc.log("Node Position: " + node.position);
x
节点 X 轴坐标。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:428 |
示例
node.x = 100;
cc.log("Node Position X: " + node.x);
y
节点 Y 轴坐标。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:471 |
示例
node.y = 100;
cc.log("Node Position Y: " + node.y);
rotation
该节点旋转角度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:514 |
示例
node.rotation = 90;
cc.log("Node Rotation: " + node.rotation);
rotationX
该节点 X 轴旋转角度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:542 |
示例
node.rotationX = 45;
cc.log("Node Rotation X: " + node.rotationX);
rotationY
该节点 Y 轴旋转角度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:568 |
示例
node.rotationY = 45;
cc.log("Node Rotation Y: " + node.rotationY);
scaleX
节点 X 轴缩放。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:594 |
示例
node.scaleX = 0.5;
cc.log("Node Scale X: " + node.scaleX);
scaleY
节点 Y 轴缩放。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:620 |
示例
node.scaleY = 0.5;
cc.log("Node Scale Y: " + node.scaleY);
skewX
该节点 Y 轴倾斜角度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:646 |
示例
node.skewX = 0;
cc.log("Node SkewX: " + node.skewX);
skewY
该节点 X 轴倾斜角度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:665 |
示例
node.skewY = 0;
cc.log("Node SkewY: " + node.skewY);
opacity
节点透明度,默认值为 255。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:684 |
示例
node.opacity = 255;
cascadeOpacity
节点的不透明度值是否影响其子节点,默认值为 true。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:711 |
示例
cc.log("CascadeOpacity: " + node.cascadeOpacity);
color
节点颜色。默认为白色,数值为:(255,255,255)。
| meta | description |
|---|---|
| 类型 | Color |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:737 |
示例
node.color = new cc.Color(255, 255, 255);
anchorX
节点 X 轴锚点位置。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:762 |
示例
node.anchorX = 0;
anchorY
节点 Y 轴锚点位置。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:787 |
示例
node.anchorY = 0;
width
节点宽度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:812 |
示例
node.width = 100;
height
节点高度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:851 |
示例
node.height = 100;
zIndex
该节点渲染排序的 Z 轴深度。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:890 |
示例
node.zIndex = 1;
cc.log("Node zIndex: " + node.zIndex);
_sgNode
Current scene graph node for this node.
| meta | description |
|---|---|
| 类型 | _ccsg.Node |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:924 |
_sizeProvider
Current active size provider for this node. Size provider can equals to this._sgNode.
| meta | description |
|---|---|
| 类型 | _ccsg.Node |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:947 |
scale
节点相对父节点的缩放。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2607 |
示例
node.scale = 1;
_components
| meta | description |
|---|---|
| 类型 | Component[] |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:132 |
_prefab
The PrefabInfo object
| meta | description |
|---|---|
| 类型 | PrefabInfo |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:141 |
_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 |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:149 |
name
该节点名称。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:173 |
示例
node.name = "New Node";
cc.log("Node Name: " + node.name);
uuid
主要用于编辑器的 uuid,在编辑器下可用于持久化存储,在项目构建之后将变成自增的 id。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:200 |
示例
cc.log("Node Uuid: " + node.uuid);
children
节点的所有子节点。
| meta | description |
|---|---|
| 类型 | Node[] |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:219 |
示例
var children = node.children;
for (var i = 0; i < children.length; ++i) {
cc.log("Node: " + children[i]);
}
childrenCount
节点的子节点数量。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:237 |
示例
var count = node.childrenCount;
cc.log("Node Children Count: " + count);
active
当前节点的自身激活状态。
值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。
如果你想检查节点在场景中实际的激活状态可以使用 Node/activeInHierarchy:property。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:253 |
示例
node.active = false;
activeInHierarchy
表示此节点是否在场景中激活。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:287 |
示例
cc.log("activeInHierarchy: " + node.activeInHierarchy);
tag
节点标签。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:301 |
示例
node.tag = 1001;
__eventTargets
Register all related EventTargets, all event callbacks will be removed in _onPreDestroy
| meta | description |
|---|---|
| 类型 | EventTarget[] |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:323 |
parent
该节点的父节点。
| meta | description |
|---|---|
| 类型 | Node |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:341 |
示例
node.parent = newNode;
_name
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:50 |
_objFlags
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:57 |
isValid
表示该对象是否可用(被销毁后将不可用)。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:225 |
示例
cc.log(obj.isValid);
方法
constructor
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:918 |
参数列表
- name String
on
在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的 this 对象。
同时您可以将事件派发到父节点或者通过调用 stopPropagation 拦截它。
推荐使用这种方式来监听节点上的触摸或鼠标事件,请不要在节点上直接使用 cc.eventManager。
| meta | description |
|---|---|
| 返回 | Function |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1151 |
参数列表
- type String 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. - 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).- event Event event
- target Object The target (this object) 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.
示例
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
删除之前与同类型,回调,目标或 useCapture 注册的回调。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1235 |
参数列表
- type String A string representing the event type being removed.
- callback Function The callback to remove.
- target Object The target (this object) 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
移除目标上的所有注册事件。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1264 |
参数列表
- target Object The target to be searched for all related callbacks
示例
node.targetOff(target);
pauseSystemEvents
暂停当前节点上注册的所有节点系统事件,节点系统事件包含触摸和鼠标事件。 如果传递 recursive 为 true,那么这个 API 将暂停本节点和它的子树上所有节点的节点系统事件。 参考:http://cocos.com/docs/creator/scripting/internal-events.html
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1279 |
参数列表
- recursive Boolean Whether to pause node system events on the sub node tree.
示例
node.pauseSystemEvents(true);
resumeSystemEvents
恢复当前节点上注册的所有节点系统事件,节点系统事件包含触摸和鼠标事件。 如果传递 recursive 为 true,那么这个 API 将恢复本节点和它的子树上所有节点的节点系统事件。 参考:http://cocos.com/docs/creator/scripting/internal-events.html
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1295 |
参数列表
- recursive Boolean Whether to resume node system events on the sub node tree.
示例
node.resumeSystemEvents(true);
runAction
执行并返回该执行的动作。该节点将会变成动作的目标。
调用 runAction 时,节点自身处于不激活状态将不会有任何效果。
注意:你不应该修改 runAction 后的动作,将无法发挥作用,如果想进行修改,请在定义 action 时加入。
| meta | description |
|---|---|
| 返回 | Action |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1430 |
参数列表
- action Action
示例
var action = cc.scaleTo(0.2, 1, 0.6);
node.runAction(action);
node.runAction(action).repeatForever(); // fail
node.runAction(action.repeatForever()); // right
pauseAllActions
暂停本节点上所有正在运行的动作。和 cc.director.getActionManager().pauseTarget(node); 等价。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1465 |
示例
node.pauseAllActions();
resumeAllActions
恢复运行本节点上所有暂停的动作。和 cc.director.getActionManager().resumeTarget(node); 等价。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1476 |
示例
node.resumeAllActions();
stopAllActions
停止并且移除所有正在运行的动作列表。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1487 |
示例
node.stopAllActions();
stopAction
停止并移除指定的动作。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1498 |
参数列表
- action Action An action object to be removed.
示例
var action = cc.scaleTo(0.2, 1, 0.6);
node.stopAction(action);
stopActionByTag
停止并且移除指定标签的动作。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1511 |
参数列表
- tag Number A tag that indicates the action to be removed.
示例
node.stopAction(1);
getActionByTag
通过标签获取指定动作。
| meta | description |
|---|---|
| 返回 | Action |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1527 |
参数列表
- tag Number
示例
var action = node.getActionByTag(1);
getNumberOfRunningActions
获取运行着的动作加上正在调度运行的动作的总数。
例如:
- 如果你正在运行 7 个动作中的 1 个 Sequence,它将返回 1。
- 如果你正在运行 2 个动作中的 7 个 Sequence,它将返回 7。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1547 |
示例
var count = node.getNumberOfRunningActions();
cc.log("Running Action Count: " + count);
getPosition
获取节点在父节点坐标系中的位置(x, y)。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1592 |
示例
cc.log("Node Position: " + node.getPosition());
setPosition
设置节点在父节点坐标系中的位置。
可以通过两种方式设置坐标点:
- 传入 2 个数值 x 和 y。
- 传入 cc.v2(x, y) 类型为 cc.Vec2 的对象。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1604 |
参数列表
- newPosOrX Vec2 | Number X coordinate for position or the position (x, y) of the node in coordinates
- y Number Y coordinate for position
示例
node.setPosition(cc.v2(0, 0));
node.setPosition(0, 0);
getScale
获取节点的缩放。当 X 轴和 Y 轴有相同的缩放数值时。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1666 |
示例
cc.log("Node Scale: " + node.getScale());
setScale
设置节点的缩放比例,默认值为 1.0。这个函数可以在同一时间修改 X 和 Y 缩放。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1682 |
参数列表
示例
node.setScale(cc.v2(1, 1));
node.setScale(1, 1);
getContentSize
获取节点自身大小,不受该节点是否被缩放或者旋转的影响。
| meta | description |
|---|---|
| 返回 | Size |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1712 |
参数列表
- ignoreSizeProvider Boolean true if you need to get the original size of the node
示例
cc.log("Content Size: " + node.getContentSize());
setContentSize
设置节点原始大小,不受该节点是否被缩放或者旋转的影响。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1735 |
参数列表
- size Size | Number The untransformed size of the node or The untransformed size's width of the node.
- height Number The untransformed size's height of the node.
示例
node.setContentSize(cc.size(100, 100));
node.setContentSize(100, 100);
setOpacityModifyRGB
设置更改透明度时是否修改RGB值,
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1779 |
参数列表
- opacityValue Boolean
示例
node.setOpacityModifyRGB(true);
isOpacityModifyRGB
更改透明度时是否修改RGB值。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1800 |
示例
var hasChange = node.isOpacityModifyRGB();
getAnchorPoint
获取节点锚点,用百分比表示。
锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1862 |
示例
cc.log("Node AnchorPoint: " + node.getAnchorPoint());
setAnchorPoint
设置锚点的百分比。
锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1886 |
参数列表
- point Vec2 | Number The anchor point of node or The x axis anchor of node.
- y Number The y axis anchor of node.
示例
node.setAnchorPoint(cc.v2(1, 1));
node.setAnchorPoint(1, 1);
getAnchorPointInPoints
返回锚点的绝对像素位置。
你只能读它。如果您要修改它,使用 setAnchorPoint。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1928 |
示例
cc.log("AnchorPointInPoints: " + node.getAnchorPointInPoints());
getDisplayedOpacity
获取节点显示透明度, 显示透明度和透明度之间的不同之处在于当启用级连透明度时, 显示透明度是基于自身透明度和父节点透明度计算的。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1945 |
示例
var displayOpacity = node.getDisplayedOpacity();
getDisplayedColor
获取节点的显示颜色, 显示颜色和颜色之间的不同之处在于当启用级连颜色时, 显示颜色是基于自身颜色和父节点颜色计算的。
| meta | description |
|---|---|
| 返回 | Color |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1975 |
示例
var displayColor = node.getDisplayedColor();
getNodeToParentTransformAR
返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。
这个矩阵以像素为单位。
该方法基于节点坐标。
| meta | description |
|---|---|
| 返回 | AffineTransform |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:1993 |
示例
var affineTransform = node.getNodeToParentTransformAR();
getBoundingBox
返回父节坐标系下的轴向对齐的包围盒。
| meta | description |
|---|---|
| 返回 | Rect |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2020 |
示例
var boundingBox = node.getBoundingBox();
getBoundingBoxToWorld
返回节点在世界坐标系下的对齐轴向的包围盒(AABB)。
该边框包含自身和已激活的子节点的世界边框。
| meta | description |
|---|---|
| 返回 | Rect |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2036 |
示例
var newRect = node.getBoundingBoxToWorld();
getNodeToParentTransform
返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。这个矩阵以像素为单位。
| meta | description |
|---|---|
| 返回 | AffineTransform |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2081 |
示例
var affineTransform = node.getNodeToParentTransform();
getNodeToWorldTransform
返回节点到世界坐标系的仿射变换矩阵。矩阵单位是像素。
| meta | description |
|---|---|
| 返回 | AffineTransform |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2104 |
示例
var affineTransform = node.getNodeToWorldTransform();
getNodeToWorldTransformAR
返回节点到世界坐标仿射变换矩阵。矩阵单位是像素。
该方法基于节点坐标。
| meta | description |
|---|---|
| 返回 | AffineTransform |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2133 |
示例
var mat = node.getNodeToWorldTransformAR();
getParentToNodeTransform
返回将父节点的坐标系转换成节点(局部)的空间坐标系的矩阵。
该矩阵以像素为单位。返回的矩阵是只读的,不能更改。
| meta | description |
|---|---|
| 返回 | AffineTransform |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2164 |
示例
var affineTransform = node.getParentToNodeTransform();
getWorldToNodeTransform
| meta | description |
|---|---|
| 返回 | AffineTransform |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2180 |
示例
var affineTransform = node.getWorldToNodeTransform();
convertToNodeSpace
将一个点转换到节点 (局部) 坐标系。结果以 Vec2 为单位。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2210 |
参数列表
- worldPoint Vec2
示例
var newVec2 = node.convertToNodeSpace(cc.v2(100, 100));
convertToWorldSpace
将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2228 |
参数列表
- nodePoint Vec2
示例
var newVec2 = node.convertToWorldSpace(cc.v2(100, 100));
convertToNodeSpaceAR
将一个点转换到节点 (局部) 空间坐标系。结果以 Vec2 为单位。
返回值将基于节点坐标。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2247 |
参数列表
- worldPoint Vec2
示例
var newVec2 = node.convertToNodeSpaceAR(cc.v2(100, 100));
convertToWorldSpaceAR
将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
返回值将基于世界坐标。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2274 |
参数列表
- nodePoint Vec2
示例
var newVec2 = node.convertToWorldSpaceAR(cc.v2(100, 100));
convertTouchToNodeSpace
将触摸点转换成本地坐标系中位置。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2301 |
参数列表
- touch Touch The touch object
示例
var newVec2 = node.convertTouchToNodeSpace(touch);
convertTouchToNodeSpaceAR
转换一个 cc.Touch(世界坐标)到一个局部坐标,该方法基于节点坐标。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2314 |
参数列表
- touch Touch The touch object
示例
var newVec2 = node.convertTouchToNodeSpaceAR(touch);
addChild
添加子节点,并且可以修改该节点的 局部 Z 顺序和标签。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2331 |
参数列表
- child Node A child node
- localZOrder Number Z order for drawing priority. Please refer to setZOrder(int)
- tag Number | String An integer or a name to identify the node easily. Please refer to setTag(int) and setName(string)
示例
node.addChild(newNode, 1, 1001);
cleanup
停止所有正在播放的动作和计时器。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2373 |
示例
node.cleanup();
sortAllChildren
根据子节点的 zIndex 和 arrivalOrder 进行排序,正常情况下开发者不需要手动调用这个函数。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2395 |
getPositionX
获取节点 X 轴坐标。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2616 |
示例
var posX = node.getPositionX();
setPositionX
设置节点 X 轴坐标。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2625 |
参数列表
- x Number
示例
node.setPositionX(1);
getPositionY
获取节点 Y 轴坐标。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2634 |
示例
var posY = node.getPositionY();
setPositionY
设置节点 Y 轴坐标。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2643 |
参数列表
- y Number The new position in y axis
示例
node.setPositionY(100);
getLocalZOrder
获取节点局部 Z 轴顺序。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2652 |
示例
var localZorder = node.getLocalZOrder();
setLocalZOrder
LocalZOrder 是 “key” (关键)来分辨节点和它兄弟节点的相关性。 父节点将会通过 LocalZOrder 的值来分辨所有的子节点。 如果两个节点有同样的 LocalZOrder,那么先加入子节点数组的节点将会显示在后加入的节点的前面。 同样的,场景图使用 “In-Order(按顺序)” 遍历数算法来遍历 ( http://en.wikipedia.org/wiki/Tree_traversal#In-order ) 并且拥有小于 0 的 LocalZOrder 的值的节点是 “ left ” 子树(左子树) 所以拥有大于 0 的 LocalZOrder 的值得节点是 “ right ”子树(右子树)。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2661 |
参数列表
- localZOrder Number
示例
node.setLocalZOrder(1);
isCascadeOpacityEnabled
返回节点的不透明度值是否影响其子节点。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2684 |
示例
cc.log(node.isCascadeOpacityEnabled());
setCascadeOpacityEnabled
启用或禁用级连不透明度,如果级连启用,子节点的不透明度将是父不透明度乘上它自己的不透明度。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCNode.js:2693 |
参数列表
- cascadeOpacityEnabled Boolean
示例
node.setCascadeOpacityEnabled(true);
attr
属性配置函数。在 attrs 的所有属性将被设置为节点属性。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:406 |
参数列表
- attrs Object Properties to be set to node
示例
var attrs = { key: 0, num: 100 };
node.attr(attrs);
getChildByTag
通过标签获取节点的子节点。
| meta | description |
|---|---|
| 返回 | Node |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:425 |
参数列表
- aTag Number An identifier to find the child node.
示例
var child = node.getChildByTag(1001);
getChildByUuid
通过 uuid 获取节点的子节点。
| meta | description |
|---|---|
| 返回 | Node |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:446 |
参数列表
- uuid String The uuid to find the child node.
示例
var child = node.getChildByUuid(uuid);
getChildByName
通过名称获取节点的子节点。
| meta | description |
|---|---|
| 返回 | Node |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:469 |
参数列表
- name String A name to find the child node.
示例
var child = node.getChildByName("Test Node");
insertChild
插入子节点到指定位置
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:507 |
参数列表
- child Node the child node to be inserted
- siblingIndex Number the sibling index to place the child in
示例
node.insertChild(child, 2);
getSiblingIndex
获取同级索引。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:525 |
示例
var index = node.getSiblingIndex();
setSiblingIndex
设置节点同级索引。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:542 |
参数列表
- index Number
示例
node.setSiblingIndex(1);
removeFromParent
从父节点中删除该节点。如果不传入 cleanup 参数或者传入 true,那么这个节点上所有绑定的事件、action 都会被删除。
因此建议调用这个 API 时总是传入 false 参数。
如果这个节点是一个孤节点,那么什么都不会发生。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:573 |
参数列表
- cleanup Boolean true if all actions and callbacks on this node should be removed, false otherwise.
示例
node.removeFromParent();
node.removeFromParent(false);
removeChild
移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
如果 cleanup 参数不传入,默认为 true 表示清理。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:597 |
参数列表
- child Node The child node which will be removed.
- cleanup Boolean true if all running actions and callbacks on the child node will be cleanup, false otherwise.
示例
node.removeChild(newNode);
node.removeChild(newNode, false);
removeChildByTag
通过标签移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
如果 cleanup 参数不传入,默认为 true 表示清理。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:625 |
参数列表
- tag Number An integer number that identifies a child node
- cleanup Boolean true if all running actions and callbacks on the child node will be cleanup, false otherwise.
示例
node.removeChildByTag(1001);
node.removeChildByTag(1001, false);
removeAllChildren
移除节点所有的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
如果 cleanup 参数不传入,默认为 true 表示清理。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:651 |
参数列表
- cleanup Boolean true if all running actions on all children nodes should be cleanup, false otherwise.
示例
node.removeAllChildren();
node.removeAllChildren(false);
isChildOf
是否是指定节点的子节点?
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:687 |
参数列表
- parent Node
示例
node.isChildOf(newNode);
getComponent
获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
传入参数也可以是脚本的名称。
| meta | description |
|---|---|
| 返回 | Component |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:710 |
参数列表
示例
// get sprite component.
var sprite = node.getComponent(cc.Sprite);
// get custom test calss.
var test = node.getComponent("Test");
getComponents
返回节点上指定类型的所有组件。
| meta | description |
|---|---|
| 返回 | Component[] |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:737 |
参数列表
示例
var sprites = node.getComponents(cc.Sprite);
var tests = node.getComponents("Test");
getComponentInChildren
递归查找所有子节点中第一个匹配指定类型的组件。
| meta | description |
|---|---|
| 返回 | Component |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:758 |
参数列表
示例
var sprite = node.getComponentInChildren(cc.Sprite);
var Test = node.getComponentInChildren("Test");
getComponentsInChildren
递归查找自身或所有子节点中指定类型的组件
| meta | description |
|---|---|
| 返回 | Component[] |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:779 |
参数列表
示例
var sprites = node.getComponentsInChildren(cc.Sprite);
var tests = node.getComponentsInChildren("Test");
addComponent
向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。
| meta | description |
|---|---|
| 返回 | Component |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:815 |
参数列表
示例
var sprite = node.addComponent(cc.Sprite);
var test = node.addComponent("Test");
_addComponentAt
This api should only used by undo system
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:903 |
参数列表
removeComponent
删除节点上的指定组件,传入参数可以是一个组件构造函数或组件名,也可以是已经获得的组件引用。 如果你已经获得组件引用,你也可以直接调用 component.destroy()
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:949 |
| 废弃(Deprecated) | please destroy the component to remove it. |
参数列表
示例
node.removeComponent(cc.Sprite);
var Test = require("Test");
node.removeComponent(Test);
_getDependComponent
| meta | description |
|---|---|
| 返回 | Component |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:977 |
参数列表
- depended Component
destroyAllChildren
销毁所有子节点,并释放所有它们对其它对象的引用。
实际销毁操作会延迟到当前帧渲染前执行。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/utils/base-node.js:1041 |
示例
node.destroyAllChildren();
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351 |
参数列表
- 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).- event Event event
- target Object The target (this object) to invoke the callback, can be null
- useCapture Boolean When set to true, the capture argument prevents callback
from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
示例
node.once(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
dispatchEvent
分发事件到事件流中。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396 |
参数列表
- event Event The Event object that is dispatched into the event flow
emit
该对象直接发送事件, 这种方法不会对事件传播到任何其他对象。
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410 |
参数列表
- message String the message to send
- detail Any whatever argument the message needs
destroy
销毁该对象,并释放所有它对其它对象的引用。
销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
实际销毁操作会延迟到当前帧渲染前执行。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:246 |
示例
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 |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:379 |
_onPreDestroy
Called before the object being destroyed.
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:412 |
_serialize
The customized serialization for this object. (Editor Only)
| meta | description |
|---|---|
| 返回 | object |
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:437 |
参数列表
- exporting Boolean
_deserialize
Init this object from the custom serialized data.
| meta | description |
|---|---|
| 定义于 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/platform/CCObject.js:447 |
参数列表
- data Object the serialized json data
- ctx _Deserializer
事件
position-changed Event
模块: cc
索引
Details
size-changed Event
模块: cc
索引
Details
anchor-changed Event
模块: cc
索引
Details
child-added Event
模块: cc
索引
Details
child-removed Event
模块: cc
索引
Details
child-reorder Event
模块: cc
索引
Details
group-changed Event
模块: cc
索引
Details
touchstart Event
模块: cc
索引
Details
active-in-hierarchy-changed Event
注意:此节点激活时,此事件仅从最顶部的节点发出。