From d1544bbdcbc4c2407b7a7576efe2fc09144ca9fc Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 22 Dec 2016 10:40:03 +0800
Subject: [PATCH 01/36] CCScene extends CCNode now
---
cocos2d/core/CCScene.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/cocos2d/core/CCScene.js b/cocos2d/core/CCScene.js
index c8d293103f..c551496327 100644
--- a/cocos2d/core/CCScene.js
+++ b/cocos2d/core/CCScene.js
@@ -36,7 +36,7 @@ var NIL = function () {};
*/
cc.Scene = cc.Class({
name: 'cc.Scene',
- extends: require('./utils/base-node'),
+ extends: require('./CCNode'),
properties: {
From 2b418d1e8bd9f9f9aa0c07e4c90b1b921f353b02 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 10:39:54 +0800
Subject: [PATCH 02/36] expand tag and parent property to functions
---
cocos2d/core/utils/base-node.js | 104 ++++++++++++++++----------------
1 file changed, 52 insertions(+), 52 deletions(-)
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 71aecbfe5c..798ce1e0a2 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -122,49 +122,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* @example
* node.parent = newNode;
*/
- parent: {
- get: function () {
- return this._parent;
- },
- set: function (value) {
- if (this._parent === value) {
- return;
- }
- if (CC_EDITOR && !cc.engine.isPlaying) {
- if (_Scene.DetectConflict.beforeAddChild(this)) {
- return;
- }
- }
- var sgNode = this._sgNode;
- if (sgNode.parent) {
- sgNode.parent.removeChild(sgNode, false);
- }
- //
- var oldParent = this._parent;
- this._parent = value || null;
- if (value) {
- var parent = value._sgNode;
- parent.addChild(sgNode);
- updateOrder(this);
- value._children.push(this);
- value.emit(CHILD_ADDED, this);
- }
- if (oldParent) {
- if (!(oldParent._objFlags & Destroying)) {
- var removeAt = oldParent._children.indexOf(this);
- if (CC_DEV && removeAt < 0) {
- return cc.errorID(1633);
- }
- oldParent._children.splice(removeAt, 1);
- oldParent.emit(CHILD_REMOVED, this);
- this._onHierarchyChanged(oldParent);
- }
- }
- else if (value) {
- this._onHierarchyChanged(null);
- }
- },
- },
_id: {
default: '',
@@ -645,15 +602,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* @example
* node.tag = 1001;
*/
- tag: {
- get: function () {
- return this._tag;
- },
- set: function (value) {
- this._tag = value;
- this._sgNode.tag = value;
- },
- },
/**
* !#en Opacity of node, default value is 255.
@@ -789,6 +737,58 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
this.__eventTargets = [];
},
+ getTag: function () {
+ return this._tag;
+ },
+
+ setTag: function (value) {
+ this._tag = value;
+ this._sgNode.tag = value;
+ },
+
+ getParent: function() {
+ return this._parent;
+ },
+
+ setParent: function (value) {
+ if (this._parent === value) {
+ return;
+ }
+ if (CC_EDITOR && !cc.engine.isPlaying) {
+ if (_Scene.DetectConflict.beforeAddChild(this)) {
+ return;
+ }
+ }
+ var sgNode = this._sgNode;
+ if (sgNode.parent) {
+ sgNode.parent.removeChild(sgNode, false);
+ }
+ //
+ var oldParent = this._parent;
+ this._parent = value || null;
+ if (value) {
+ var parent = value._sgNode;
+ parent.addChild(sgNode);
+ updateOrder(this);
+ value._children.push(this);
+ value.emit(CHILD_ADDED, this);
+ }
+ if (oldParent) {
+ if (!(oldParent._objFlags & Destroying)) {
+ var removeAt = oldParent._children.indexOf(this);
+ if (CC_DEV && removeAt < 0) {
+ return cc.errorID(1633);
+ }
+ oldParent._children.splice(removeAt, 1);
+ oldParent.emit(CHILD_REMOVED, this);
+ this._onHierarchyChanged(oldParent);
+ }
+ }
+ else if (value) {
+ this._onHierarchyChanged(null);
+ }
+ },
+
_onPreDestroy: function () {
cc.eventManager.removeListeners(this);
if (CC_JSB) {
From 55aaef12908c0a5405f0fb8e8f79afbd1e868472 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 10:49:00 +0800
Subject: [PATCH 03/36] move comments to CCNode
---
cocos2d/core/CCNode.js | 114 +++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 115 --------------------------------
2 files changed, 114 insertions(+), 115 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 98e26aa366..c5c88c4fa2 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -1560,6 +1560,120 @@ if (CC_JSB) {
*
*/
+/**
+ * !#en The local scale relative to the parent.
+ * !#zh 节点相对父节点的缩放。
+ * @property scale
+ * @type {Number}
+ * @example
+ * node.scale = 1;
+ */
+
+/**
+ * !#en Returns the x axis position of the node in cocos2d coordinates.
+ * !#zh 获取节点 X 轴坐标。
+ * @method getPositionX
+ * @return {Number} x - The new position in x axis
+ * @example
+ * var posX = node.getPositionX();
+ */
+
+/**
+ * !#en Sets the x axis position of the node in cocos2d coordinates.
+ * !#zh 设置节点 X 轴坐标。
+ * @method setPositionX
+ * @param {Number} x
+ * @example
+ * node.setPositionX(1);
+ */
+
+/**
+ * !#en Returns the y axis position of the node in cocos2d coordinates.
+ * !#zh 获取节点 Y 轴坐标。
+ * @method getPositionY
+ * @return {Number}
+ * @example
+ * var posY = node.getPositionY();
+ */
+
+/**
+ * !#en Sets the y axis position of the node in cocos2d coordinates.
+ * !#zh 设置节点 Y 轴坐标。
+ * @method setPositionY
+ * @param {Number} y - The new position in y axis
+ * @example
+ * node.setPositionY(100);
+ */
+
+/**
+ * !#en Returns the local Z order of this node.
+ * !#zh 获取节点局部 Z 轴顺序。
+ * @method getLocalZOrder
+ * @returns {Number} The local (relative to its siblings) Z order.
+ * @example
+ * var localZorder = node.getLocalZOrder();
+ */
+
+/**
+ * !#en
+ * 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.
+ * !#zh
+ * LocalZOrder 是 “key” (关键)来分辨节点和它兄弟节点的相关性。
+ * 父节点将会通过 LocalZOrder 的值来分辨所有的子节点。
+ * 如果两个节点有同样的 LocalZOrder,那么先加入子节点数组的节点将会显示在后加入的节点的前面。
+ * 同样的,场景图使用 “In-Order(按顺序)” 遍历数算法来遍历
+ * ( http://en.wikipedia.org/wiki/Tree_traversal#In-order ) 并且拥有小于 0 的 LocalZOrder 的值的节点是 “ left ” 子树(左子树)
+ * 所以拥有大于 0 的 LocalZOrder 的值得节点是 “ right ”子树(右子树)。
+ * @method setLocalZOrder
+ * @param {Number} localZOrder
+ * @example
+ * node.setLocalZOrder(1);
+ */
+
+/**
+ * !#en Returns whether node's opacity value affect its child nodes.
+ * !#zh 返回节点的不透明度值是否影响其子节点。
+ * @method isCascadeOpacityEnabled
+ * @returns {Boolean}
+ * @example
+ * cc.log(node.isCascadeOpacityEnabled());
+ */
+
+/**
+ * !#en Enable or disable cascade opacity, if cascade enabled, child nodes' opacity will be the multiplication of parent opacity and its own opacity.
+ * !#zh 启用或禁用级连不透明度,如果级连启用,子节点的不透明度将是父不透明度乘上它自己的不透明度。
+ * @method setCascadeOpacityEnabled
+ * @param {Boolean} cascadeOpacityEnabled
+ * @example
+ * node.setCascadeOpacityEnabled(true);
+ */
+
+/*
+ * !#en Returns whether node's color value affect its child nodes.
+ * !#zh 返回节点的颜色值是否影响其子节点。
+ * @method isCascadeColorEnabled
+ * @returns {Boolean}
+ * @example
+ * cc.log(node.isCascadeColorEnabled());
+ */
+
+/**
+ * !#en Enable or disable cascade color, if cascade enabled, child nodes' opacity will be the cascade value of parent color and its own color.
+ * !#zh 启用或禁用级连颜色,如果级连启用,子节点的颜色将是父颜色和它自己的颜色的级连值。
+ * @method setCascadeColorEnabled
+ * @param {Boolean} cascadeColorEnabled
+ * @example
+ * node.setCascadeColorEnabled(true);
+ */
+
+
Node.EventType = EventType;
cc.Node = module.exports = Node;
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 798ce1e0a2..233705d31c 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -2087,119 +2087,4 @@ var DiffNameGetSets = {
};
Misc.propertyDefine(BaseNode, SameNameGetSets, DiffNameGetSets);
-
-/**
- * !#en The local scale relative to the parent.
- * !#zh 节点相对父节点的缩放。
- * @property scale
- * @type {Number}
- * @example
- * node.scale = 1;
- */
-
-/**
- * !#en Returns the x axis position of the node in cocos2d coordinates.
- * !#zh 获取节点 X 轴坐标。
- * @method getPositionX
- * @return {Number} x - The new position in x axis
- * @example
- * var posX = node.getPositionX();
- */
-
-/**
- * !#en Sets the x axis position of the node in cocos2d coordinates.
- * !#zh 设置节点 X 轴坐标。
- * @method setPositionX
- * @param {Number} x
- * @example
- * node.setPositionX(1);
- */
-
-/**
- * !#en Returns the y axis position of the node in cocos2d coordinates.
- * !#zh 获取节点 Y 轴坐标。
- * @method getPositionY
- * @return {Number}
- * @example
- * var posY = node.getPositionY();
- */
-
-/**
- * !#en Sets the y axis position of the node in cocos2d coordinates.
- * !#zh 设置节点 Y 轴坐标。
- * @method setPositionY
- * @param {Number} y - The new position in y axis
- * @example
- * node.setPositionY(100);
- */
-
-/**
- * !#en Returns the local Z order of this node.
- * !#zh 获取节点局部 Z 轴顺序。
- * @method getLocalZOrder
- * @returns {Number} The local (relative to its siblings) Z order.
- * @example
- * var localZorder = node.getLocalZOrder();
- */
-
-/**
- * !#en
- * 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.
- * !#zh
- * LocalZOrder 是 “key” (关键)来分辨节点和它兄弟节点的相关性。
- * 父节点将会通过 LocalZOrder 的值来分辨所有的子节点。
- * 如果两个节点有同样的 LocalZOrder,那么先加入子节点数组的节点将会显示在后加入的节点的前面。
- * 同样的,场景图使用 “In-Order(按顺序)” 遍历数算法来遍历
- * ( http://en.wikipedia.org/wiki/Tree_traversal#In-order ) 并且拥有小于 0 的 LocalZOrder 的值的节点是 “ left ” 子树(左子树)
- * 所以拥有大于 0 的 LocalZOrder 的值得节点是 “ right ”子树(右子树)。
- * @method setLocalZOrder
- * @param {Number} localZOrder
- * @example
- * node.setLocalZOrder(1);
- */
-
-/**
- * !#en Returns whether node's opacity value affect its child nodes.
- * !#zh 返回节点的不透明度值是否影响其子节点。
- * @method isCascadeOpacityEnabled
- * @returns {Boolean}
- * @example
- * cc.log(node.isCascadeOpacityEnabled());
- */
-
-/**
- * !#en Enable or disable cascade opacity, if cascade enabled, child nodes' opacity will be the multiplication of parent opacity and its own opacity.
- * !#zh 启用或禁用级连不透明度,如果级连启用,子节点的不透明度将是父不透明度乘上它自己的不透明度。
- * @method setCascadeOpacityEnabled
- * @param {Boolean} cascadeOpacityEnabled
- * @example
- * node.setCascadeOpacityEnabled(true);
- */
-
-/*
- * !#en Returns whether node's color value affect its child nodes.
- * !#zh 返回节点的颜色值是否影响其子节点。
- * @method isCascadeColorEnabled
- * @returns {Boolean}
- * @example
- * cc.log(node.isCascadeColorEnabled());
- */
-
-/**
- * !#en Enable or disable cascade color, if cascade enabled, child nodes' opacity will be the cascade value of parent color and its own color.
- * !#zh 启用或禁用级连颜色,如果级连启用,子节点的颜色将是父颜色和它自己的颜色的级连值。
- * @method setCascadeColorEnabled
- * @param {Boolean} cascadeColorEnabled
- * @example
- * node.setCascadeColorEnabled(true);
- */
-
-
cc._BaseNode = module.exports = BaseNode;
From ac259792e1f2404ef3bc8f82ec9560dfd4bfe591 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 11:06:25 +0800
Subject: [PATCH 04/36] move
skewX/skewY/rotation/rotationX/rotationY/scale/scaleX/scaleY to CCNode
---
cocos2d/core/CCNode.js | 204 +++++++++++++++++++++++++++++++-
cocos2d/core/utils/base-node.js | 189 +----------------------------
2 files changed, 204 insertions(+), 189 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index c5c88c4fa2..c1ff4070f2 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -31,6 +31,8 @@ var Flags = cc.Object.Flags;
var Destroying = Flags.Destroying;
var DontDestroy = Flags.DontDestroy;
var Activating = Flags.Activating;
+
+var Misc = require('./utils/misc');
//var RegisteredInEditor = Flags.RegisteredInEditor;
/**
@@ -452,7 +454,156 @@ var Node = cc.Class({
this.groupIndex = cc.game.groupList.indexOf(value);
this.emit('group-changed');
}
- }
+ },
+
+ //properties moved from base node begin
+
+ /**
+ * !#en Rotation of node.
+ * !#zh 该节点旋转角度。
+ * @property rotation
+ * @type {Number}
+ * @example
+ * node.rotation = 90;
+ * cc.log("Node Rotation: " + node.rotation);
+ */
+ rotation: {
+ get: function () {
+ if (this._rotationX !== this._rotationY)
+ cc.logID(1602);
+ return this._rotationX;
+ },
+ set: function (value) {
+ if (this._rotationX !== value || this._rotationY !== value ) {
+ this._rotationX = this._rotationY = value;
+ this._sgNode.rotation = value;
+ }
+ }
+ },
+
+ /**
+ * !#en Rotation on x axis.
+ * !#zh 该节点 X 轴旋转角度。
+ * @property rotationX
+ * @type {Number}
+ * @example
+ * node.rotationX = 45;
+ * cc.log("Node Rotation X: " + node.rotationX);
+ */
+ rotationX: {
+ get: function () {
+ return this._rotationX;
+ },
+ set: function (value) {
+ if (this._rotationX !== value) {
+ this._rotationX = value;
+ this._sgNode.rotationX = value;
+ }
+ },
+ },
+
+ /**
+ * !#en Rotation on y axis.
+ * !#zh 该节点 Y 轴旋转角度。
+ * @property rotationY
+ * @type {Number}
+ * @example
+ * node.rotationY = 45;
+ * cc.log("Node Rotation Y: " + node.rotationY);
+ */
+ rotationY: {
+ get: function () {
+ return this._rotationY;
+ },
+ set: function (value) {
+ if (this._rotationY !== value) {
+ this._rotationY = value;
+ this._sgNode.rotationY = value;
+ }
+ },
+ },
+
+ /**
+ * !#en Scale on x axis.
+ * !#zh 节点 X 轴缩放。
+ * @property scaleX
+ * @type {Number}
+ * @example
+ * node.scaleX = 0.5;
+ * cc.log("Node Scale X: " + node.scaleX);
+ */
+ scaleX: {
+ get: function () {
+ return this._scaleX;
+ },
+ set: function (value) {
+ if (this._scaleX !== value) {
+ this._scaleX = value;
+ this._sgNode.scaleX = value;
+ }
+ },
+ },
+
+ /**
+ * !#en Scale on y axis.
+ * !#zh 节点 Y 轴缩放。
+ * @property scaleY
+ * @type {Number}
+ * @example
+ * node.scaleY = 0.5;
+ * cc.log("Node Scale Y: " + node.scaleY);
+ */
+ scaleY: {
+ get: function () {
+ return this._scaleY;
+ },
+ set: function (value) {
+ if (this._scaleY !== value) {
+ this._scaleY = value;
+ this._sgNode.scaleY = value;
+ }
+ },
+ },
+
+ /**
+ * !#en Skew x
+ * !#zh 该节点 Y 轴倾斜角度。
+ * @property skewX
+ * @type {Number}
+ * @example
+ * node.skewX = 0;
+ * cc.log("Node SkewX: " + node.skewX);
+ */
+ skewX: {
+ get: function () {
+ return this._skewX;
+ },
+ set: function (value) {
+ this._skewX = value;
+ this._sgNode.skewX = value;
+ }
+ },
+
+ /**
+ * !#en Skew y
+ * !#zh 该节点 X 轴倾斜角度。
+ * @property skewY
+ * @type {Number}
+ * @example
+ * node.skewY = 0;
+ * cc.log("Node SkewY: " + node.skewY);
+ */
+ skewY: {
+ get: function () {
+ return this._skewY;
+ },
+ set: function (value) {
+ this._skewY = value;
+ this._sgNode.skewY = value;
+ }
+ },
+
+ //properties moved from base node end
},
ctor: function (name) {
@@ -1475,6 +1626,51 @@ var Node = cc.Class({
}
},
+ //functions moved from base node begin
+
+ /**
+ * !#en
+ * Returns the scale factor of the node.
+ * Assertion will fail when _scaleX != _scaleY.
+ * !#zh 获取节点的缩放。当 X 轴和 Y 轴有相同的缩放数值时。
+ * @method getScale
+ * @return {Number} The scale factor
+ * @example
+ * cc.log("Node Scale: " + node.getScale());
+ */
+ getScale: function () {
+ if (this._scaleX !== this._scaleY)
+ cc.logID(1603);
+ return this._scaleX;
+ },
+
+ /**
+ * !#en 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.
+ * !#zh 设置节点的缩放比例,默认值为 1.0。这个函数可以在同一时间修改 X 和 Y 缩放。
+ * @method setScale
+ * @param {Number|Vec2} scaleX - scaleX or scale
+ * @param {Number} [scaleY=scale]
+ * @example
+ * node.setScale(cc.v2(1, 1));
+ * node.setScale(1, 1);
+ */
+ setScale: function (scaleX, scaleY) {
+ if (typeof scaleX === 'object') {
+ scaleY = scaleX.y;
+ scaleX = scaleX.x
+ }
+ else {
+ scaleY = (scaleY || scaleY === 0) ? scaleY : scaleX;
+ }
+ if (this._scaleX !== scaleX || this._scaleY !== scaleY) {
+ this._scaleX = scaleX;
+ this._scaleY = scaleY;
+ this._sgNode.setScale(scaleX, scaleY);
+ }
+ },
+
+ //functions moved from base node end
+
});
// In JSB, when inner sg node being replaced, the system event listeners will be cleared.
@@ -1673,6 +1869,12 @@ if (CC_JSB) {
* node.setCascadeColorEnabled(true);
*/
+var SameNameGetSets = ['skewX', 'skewY', 'rotation', 'rotationX', 'rotationY', 'scale', 'scaleX', 'scaleY', ];
+
+var DiffNameGetSets = {
+};
+
+Misc.propertyDefine(Node, SameNameGetSets, DiffNameGetSets);
Node.EventType = EventType;
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 233705d31c..cf66d914ed 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -147,44 +147,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /**
- * !#en Skew x
- * !#zh 该节点 Y 轴倾斜角度。
- * @property skewX
- * @type {Number}
- * @example
- * node.skewX = 0;
- * cc.log("Node SkewX: " + node.skewX);
- */
- skewX: {
- get: function () {
- return this._skewX;
- },
- set: function (value) {
- this._skewX = value;
- this._sgNode.skewX = value;
- }
- },
-
- /**
- * !#en Skew y
- * !#zh 该节点 X 轴倾斜角度。
- * @property skewY
- * @type {Number}
- * @example
- * node.skewY = 0;
- * cc.log("Node SkewY: " + node.skewY);
- */
- skewY: {
- get: function () {
- return this._skewY;
- },
- set: function (value) {
- this._skewY = value;
- this._sgNode.skewY = value;
- }
- },
-
/**
* !#en Z order in depth which stands for the drawing order.
* !#zh 该节点渲染排序的 Z 轴深度。
@@ -210,113 +172,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /**
- * !#en Rotation of node.
- * !#zh 该节点旋转角度。
- * @property rotation
- * @type {Number}
- * @example
- * node.rotation = 90;
- * cc.log("Node Rotation: " + node.rotation);
- */
- rotation: {
- get: function () {
- if (this._rotationX !== this._rotationY)
- cc.logID(1602);
- return this._rotationX;
- },
- set: function (value) {
- if (this._rotationX !== value || this._rotationY !== value ) {
- this._rotationX = this._rotationY = value;
- this._sgNode.rotation = value;
- }
- }
- },
-
- /**
- * !#en Rotation on x axis.
- * !#zh 该节点 X 轴旋转角度。
- * @property rotationX
- * @type {Number}
- * @example
- * node.rotationX = 45;
- * cc.log("Node Rotation X: " + node.rotationX);
- */
- rotationX: {
- get: function () {
- return this._rotationX;
- },
- set: function (value) {
- if (this._rotationX !== value) {
- this._rotationX = value;
- this._sgNode.rotationX = value;
- }
- },
- },
-
- /**
- * !#en Rotation on y axis.
- * !#zh 该节点 Y 轴旋转角度。
- * @property rotationY
- * @type {Number}
- * @example
- * node.rotationY = 45;
- * cc.log("Node Rotation Y: " + node.rotationY);
- */
- rotationY: {
- get: function () {
- return this._rotationY;
- },
- set: function (value) {
- if (this._rotationY !== value) {
- this._rotationY = value;
- this._sgNode.rotationY = value;
- }
- },
- },
-
- /**
- * !#en Scale on x axis.
- * !#zh 节点 X 轴缩放。
- * @property scaleX
- * @type {Number}
- * @example
- * node.scaleX = 0.5;
- * cc.log("Node Scale X: " + node.scaleX);
- */
- scaleX: {
- get: function () {
- return this._scaleX;
- },
- set: function (value) {
- if (this._scaleX !== value) {
- this._scaleX = value;
- this._sgNode.scaleX = value;
- }
- },
- },
-
- /**
- * !#en Scale on y axis.
- * !#zh 节点 Y 轴缩放。
- * @property scaleY
- * @type {Number}
- * @example
- * node.scaleY = 0.5;
- * cc.log("Node Scale Y: " + node.scaleY);
- */
- scaleY: {
- get: function () {
- return this._scaleY;
- },
- set: function (value) {
- if (this._scaleY !== value) {
- this._scaleY = value;
- this._sgNode.scaleY = value;
- }
- },
- },
-
/**
* !#en x axis position of node.
* !#zh 节点 X 轴坐标。
@@ -886,47 +741,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return this._globalZOrder;
},
- /**
- * !#en
- * Returns the scale factor of the node.
- * Assertion will fail when _scaleX != _scaleY.
- * !#zh 获取节点的缩放。当 X 轴和 Y 轴有相同的缩放数值时。
- * @method getScale
- * @return {Number} The scale factor
- * @example
- * cc.log("Node Scale: " + node.getScale());
- */
- getScale: function () {
- if (this._scaleX !== this._scaleY)
- cc.logID(1603);
- return this._scaleX;
- },
-
- /**
- * !#en 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.
- * !#zh 设置节点的缩放比例,默认值为 1.0。这个函数可以在同一时间修改 X 和 Y 缩放。
- * @method setScale
- * @param {Number|Vec2} scaleX - scaleX or scale
- * @param {Number} [scaleY=scale]
- * @example
- * node.setScale(cc.v2(1, 1));
- * node.setScale(1, 1);
- */
- setScale: function (scaleX, scaleY) {
- if (typeof scaleX === 'object') {
- scaleY = scaleX.y;
- scaleX = scaleX.x
- }
- else {
- scaleY = (scaleY || scaleY === 0) ? scaleY : scaleX;
- }
- if (this._scaleX !== scaleX || this._scaleY !== scaleY) {
- this._scaleX = scaleX;
- this._scaleY = scaleY;
- this._sgNode.setScale(scaleX, scaleY);
- }
- },
-
/**
* !#en Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.
* !#zh 获取在父节点坐标系中节点的位置( x , y )。
@@ -2070,8 +1884,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
// Define public getter and setter methods to ensure api compatibility.
-var SameNameGetSets = ['name', 'skewX', 'skewY', 'position', 'rotation', 'rotationX', 'rotationY',
- 'scale', 'scaleX', 'scaleY', 'children', 'childrenCount', 'parent',
+var SameNameGetSets = ['name', 'position', 'children', 'childrenCount', 'parent',
/*'shaderProgram',*/ 'opacity', 'color', 'tag'];
var DiffNameGetSets = {
x: ['getPositionX', 'setPositionX'],
From 773fdd12d1268071e606fec1b8f97c552f3818e1 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 11:19:12 +0800
Subject: [PATCH 05/36] move position/x/y to CCNode
---
cocos2d/core/CCNode.js | 184 +++++++++++++++++++++++++++++++-
cocos2d/core/utils/base-node.js | 175 +-----------------------------
2 files changed, 184 insertions(+), 175 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index c1ff4070f2..44a843fd1c 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -32,6 +32,15 @@ var Destroying = Flags.Destroying;
var DontDestroy = Flags.DontDestroy;
var Activating = Flags.Activating;
+var POSITION_CHANGED = 'position-changed';
+var SIZE_CHANGED = 'size-changed';
+var ANCHOR_CHANGED = 'anchor-changed';
+var CHILD_ADDED = 'child-added';
+var CHILD_REMOVED = 'child-removed';
+var CHILD_REORDER = 'child-reorder';
+
+var ERR_INVALID_NUMBER = CC_EDITOR && 'The %s is invalid';
+
var Misc = require('./utils/misc');
//var RegisteredInEditor = Flags.RegisteredInEditor;
@@ -458,6 +467,98 @@ var Node = cc.Class({
//properties moved from base node begin
+ /**
+ * !#en x axis position of node.
+ * !#zh 节点 X 轴坐标。
+ * @property x
+ * @type {Number}
+ * @example
+ * node.x = 100;
+ * cc.log("Node Position X: " + node.x);
+ */
+ x: {
+ get: function () {
+ return this._position.x;
+ },
+ set: function (value) {
+ var localPosition = this._position;
+ if (value !== localPosition.x) {
+ if (!CC_EDITOR || isFinite(value)) {
+ if (CC_EDITOR) {
+ var oldValue = localPosition.x;
+ }
+
+ localPosition.x = value;
+ this._sgNode.setPositionX(value);
+
+ // fast check event
+ var capListeners = this._capturingListeners &&
+ this._capturingListeners._callbackTable[POSITION_CHANGED];
+ var bubListeners = this._bubblingListeners &&
+ this._bubblingListeners._callbackTable[POSITION_CHANGED];
+ if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
+ // send event
+ if (CC_EDITOR) {
+ this.emit(POSITION_CHANGED, new cc.Vec2(oldValue, localPosition.y));
+ }
+ else {
+ this.emit(POSITION_CHANGED);
+ }
+ }
+ }
+ else {
+ cc.error(ERR_INVALID_NUMBER, 'new x');
+ }
+ }
+ },
+ },
+
+ /**
+ * !#en y axis position of node.
+ * !#zh 节点 Y 轴坐标。
+ * @property y
+ * @type {Number}
+ * @example
+ * node.y = 100;
+ * cc.log("Node Position Y: " + node.y);
+ */
+ y: {
+ get: function () {
+ return this._position.y;
+ },
+ set: function (value) {
+ var localPosition = this._position;
+ if (value !== localPosition.y) {
+ if (!CC_EDITOR || isFinite(value)) {
+ if (CC_EDITOR) {
+ var oldValue = localPosition.y;
+ }
+
+ localPosition.y = value;
+ this._sgNode.setPositionY(value);
+
+ // fast check event
+ var capListeners = this._capturingListeners &&
+ this._capturingListeners._callbackTable[POSITION_CHANGED];
+ var bubListeners = this._bubblingListeners &&
+ this._bubblingListeners._callbackTable[POSITION_CHANGED];
+ if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
+ // send event
+ if (CC_EDITOR) {
+ this.emit(POSITION_CHANGED, new cc.Vec2(localPosition.x, oldValue));
+ }
+ else {
+ this.emit(POSITION_CHANGED);
+ }
+ }
+ }
+ else {
+ cc.error(ERR_INVALID_NUMBER, 'new y');
+ }
+ }
+ },
+ },
+
/**
* !#en Rotation of node.
* !#zh 该节点旋转角度。
@@ -1628,6 +1729,85 @@ var Node = cc.Class({
//functions moved from base node begin
+ /**
+ * !#en Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.
+ * !#zh 获取在父节点坐标系中节点的位置( x , y )。
+ * @method getPosition
+ * @return {Vec2} The position (x,y) of the node in OpenGL coordinates
+ * @example
+ * cc.log("Node Position: " + node.getPosition());
+ */
+ getPosition: function () {
+ return cc.p(this._position);
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 设置节点在父坐标系中的位置。
+ * 可以通过 2 种方式设置坐标点:
+ * 1.传入 cc.v2(x, y) 类型为 cc.Vec2 的对象。
+ * 2.传入 2 个数值 x 和 y。
+ * @method setPosition
+ * @param {Vec2|Number} newPosOrxValue - The position (x,y) of the node in coordinates or the X coordinate for position
+ * @param {Number} [yValue] - Y coordinate for position
+ * @example {@link utils/api/engine/docs/cocos2d/core/utils/base-node/setPosition.js}
+ */
+ setPosition: function (newPosOrxValue, yValue) {
+ var xValue;
+ if (typeof yValue === 'undefined') {
+ xValue = newPosOrxValue.x;
+ yValue = newPosOrxValue.y;
+ }
+ else {
+ xValue = newPosOrxValue;
+ yValue = yValue;
+ }
+
+ var locPosition = this._position;
+ if(locPosition.x === xValue && locPosition.y === yValue) {
+ return;
+ }
+
+ if (CC_EDITOR) {
+ var oldPosition = new cc.Vec2(locPosition);
+ }
+
+ if (!CC_EDITOR || isFinite(xValue)) {
+ locPosition.x = xValue;
+ }
+ else {
+ return cc.error(ERR_INVALID_NUMBER, 'x of new position');
+ }
+ if (!CC_EDITOR || isFinite(yValue)) {
+ locPosition.y = yValue;
+ }
+ else {
+ return cc.error(ERR_INVALID_NUMBER, 'y of new position');
+ }
+
+ this._sgNode.setPosition(xValue, yValue);
+
+ // fast check event
+ var capListeners = this._capturingListeners &&
+ this._capturingListeners._callbackTable[POSITION_CHANGED];
+ var bubListeners = this._bubblingListeners &&
+ this._bubblingListeners._callbackTable[POSITION_CHANGED];
+ if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
+ // send event
+ if (CC_EDITOR) {
+ this.emit(POSITION_CHANGED, oldPosition);
+ }
+ else {
+ this.emit(POSITION_CHANGED);
+ }
+ }
+ },
+
/**
* !#en
* Returns the scale factor of the node.
@@ -1869,9 +2049,11 @@ if (CC_JSB) {
* node.setCascadeColorEnabled(true);
*/
-var SameNameGetSets = ['skewX', 'skewY', 'rotation', 'rotationX', 'rotationY', 'scale', 'scaleX', 'scaleY', ];
+var SameNameGetSets = ['skewX', 'skewY', 'position', 'rotation', 'rotationX', 'rotationY', 'scale', 'scaleX', 'scaleY', ];
var DiffNameGetSets = {
+ x: ['getPositionX', 'setPositionX'],
+ y: ['getPositionY', 'setPositionY'],
};
Misc.propertyDefine(Node, SameNameGetSets, DiffNameGetSets);
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index cf66d914ed..2700529e22 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -172,98 +172,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /**
- * !#en x axis position of node.
- * !#zh 节点 X 轴坐标。
- * @property x
- * @type {Number}
- * @example
- * node.x = 100;
- * cc.log("Node Position X: " + node.x);
- */
- x: {
- get: function () {
- return this._position.x;
- },
- set: function (value) {
- var localPosition = this._position;
- if (value !== localPosition.x) {
- if (!CC_EDITOR || isFinite(value)) {
- if (CC_EDITOR) {
- var oldValue = localPosition.x;
- }
-
- localPosition.x = value;
- this._sgNode.setPositionX(value);
-
- // fast check event
- var capListeners = this._capturingListeners &&
- this._capturingListeners._callbackTable[POSITION_CHANGED];
- var bubListeners = this._bubblingListeners &&
- this._bubblingListeners._callbackTable[POSITION_CHANGED];
- if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
- // send event
- if (CC_EDITOR) {
- this.emit(POSITION_CHANGED, new cc.Vec2(oldValue, localPosition.y));
- }
- else {
- this.emit(POSITION_CHANGED);
- }
- }
- }
- else {
- cc.error(ERR_INVALID_NUMBER, 'new x');
- }
- }
- },
- },
-
- /**
- * !#en y axis position of node.
- * !#zh 节点 Y 轴坐标。
- * @property y
- * @type {Number}
- * @example
- * node.y = 100;
- * cc.log("Node Position Y: " + node.y);
- */
- y: {
- get: function () {
- return this._position.y;
- },
- set: function (value) {
- var localPosition = this._position;
- if (value !== localPosition.y) {
- if (!CC_EDITOR || isFinite(value)) {
- if (CC_EDITOR) {
- var oldValue = localPosition.y;
- }
-
- localPosition.y = value;
- this._sgNode.setPositionY(value);
-
- // fast check event
- var capListeners = this._capturingListeners &&
- this._capturingListeners._callbackTable[POSITION_CHANGED];
- var bubListeners = this._bubblingListeners &&
- this._bubblingListeners._callbackTable[POSITION_CHANGED];
- if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
- // send event
- if (CC_EDITOR) {
- this.emit(POSITION_CHANGED, new cc.Vec2(localPosition.x, oldValue));
- }
- else {
- this.emit(POSITION_CHANGED);
- }
- }
- }
- else {
- cc.error(ERR_INVALID_NUMBER, 'new y');
- }
- }
- },
- },
-
/**
* !#en All children nodes.
* !#zh 节点的所有子节点。
@@ -741,85 +649,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return this._globalZOrder;
},
- /**
- * !#en Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.
- * !#zh 获取在父节点坐标系中节点的位置( x , y )。
- * @method getPosition
- * @return {Vec2} The position (x,y) of the node in OpenGL coordinates
- * @example
- * cc.log("Node Position: " + node.getPosition());
- */
- getPosition: function () {
- return cc.p(this._position);
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 设置节点在父坐标系中的位置。
- * 可以通过 2 种方式设置坐标点:
- * 1.传入 cc.v2(x, y) 类型为 cc.Vec2 的对象。
- * 2.传入 2 个数值 x 和 y。
- * @method setPosition
- * @param {Vec2|Number} newPosOrxValue - The position (x,y) of the node in coordinates or the X coordinate for position
- * @param {Number} [yValue] - Y coordinate for position
- * @example {@link utils/api/engine/docs/cocos2d/core/utils/base-node/setPosition.js}
- */
- setPosition: function (newPosOrxValue, yValue) {
- var xValue;
- if (typeof yValue === 'undefined') {
- xValue = newPosOrxValue.x;
- yValue = newPosOrxValue.y;
- }
- else {
- xValue = newPosOrxValue;
- yValue = yValue;
- }
-
- var locPosition = this._position;
- if(locPosition.x === xValue && locPosition.y === yValue) {
- return;
- }
-
- if (CC_EDITOR) {
- var oldPosition = new cc.Vec2(locPosition);
- }
-
- if (!CC_EDITOR || isFinite(xValue)) {
- locPosition.x = xValue;
- }
- else {
- return cc.error(ERR_INVALID_NUMBER, 'x of new position');
- }
- if (!CC_EDITOR || isFinite(yValue)) {
- locPosition.y = yValue;
- }
- else {
- return cc.error(ERR_INVALID_NUMBER, 'y of new position');
- }
-
- this._sgNode.setPosition(xValue, yValue);
-
- // fast check event
- var capListeners = this._capturingListeners &&
- this._capturingListeners._callbackTable[POSITION_CHANGED];
- var bubListeners = this._bubblingListeners &&
- this._bubblingListeners._callbackTable[POSITION_CHANGED];
- if ((capListeners && capListeners.length > 0) || (bubListeners && bubListeners.length > 0)) {
- // send event
- if (CC_EDITOR) {
- this.emit(POSITION_CHANGED, oldPosition);
- }
- else {
- this.emit(POSITION_CHANGED);
- }
- }
- },
-
/**
* !#en
* Returns a copy of the anchor point.
@@ -1884,11 +1713,9 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
// Define public getter and setter methods to ensure api compatibility.
-var SameNameGetSets = ['name', 'position', 'children', 'childrenCount', 'parent',
+var SameNameGetSets = ['name', 'children', 'childrenCount', 'parent',
/*'shaderProgram',*/ 'opacity', 'color', 'tag'];
var DiffNameGetSets = {
- x: ['getPositionX', 'setPositionX'],
- y: ['getPositionY', 'setPositionY'],
zIndex: ['getLocalZOrder', 'setLocalZOrder'],
opacityModifyRGB: ['isOpacityModifyRGB'],
cascadeOpacity: ['isCascadeOpacityEnabled', 'setCascadeOpacityEnabled'],
From acc7d4061ed98a20e35d65c660581934be14a921 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 11:39:12 +0800
Subject: [PATCH 06/36] move opacity/color/cascadeOpacity to CCNode
---
cocos2d/core/CCNode.js | 86 ++++++++++++++++++++++++++++++++-
cocos2d/core/utils/base-node.js | 84 +-------------------------------
2 files changed, 86 insertions(+), 84 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 44a843fd1c..fd2896c8c1 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -704,6 +704,88 @@ var Node = cc.Class({
}
},
+ /**
+ * !#en Opacity of node, default value is 255.
+ * !#zh 节点透明度,默认值为 255。
+ * @property opacity
+ * @type {Number}
+ * @example
+ * node.opacity = 255;
+ */
+ opacity: {
+ get: function () {
+ return this._opacity;
+ },
+ set: function (value) {
+ if (this._opacity !== value) {
+ this._opacity = value;
+ this._sgNode.setOpacity(value);
+ if (!this._cascadeOpacityEnabled) {
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
+ sizeProvider.setOpacity(value);
+ }
+ }
+ }
+ },
+ range: [0, 255]
+ },
+
+ /**
+ * !#en Indicate whether node's opacity value affect its child nodes, default value is true.
+ * !#zh 节点的不透明度值是否影响其子节点,默认值为 true。
+ * @property cascadeOpacity
+ * @type {Boolean}
+ * @example
+ * cc.log("CascadeOpacity: " + node.cascadeOpacity);
+ */
+ cascadeOpacity: {
+ get: function () {
+ return this._cascadeOpacityEnabled;
+ },
+ set: function (value) {
+ if (this._cascadeOpacityEnabled !== value) {
+ this._cascadeOpacityEnabled = value;
+ this._sgNode.cascadeOpacity = value;
+
+ var opacity = value ? 255 : this._opacity;
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider instanceof _ccsg.Node) {
+ sizeProvider.setOpacity(opacity);
+ }
+ }
+ },
+ },
+
+ /**
+ * !#en Color of node, default value is white: (255, 255, 255).
+ * !#zh 节点颜色。默认为白色,数值为:(255,255,255)。
+ * @property color
+ * @type {Color}
+ * @example
+ * node.color = new cc.Color(255, 255, 255);
+ */
+ color: {
+ get: function () {
+ var color = this._color;
+ return new cc.Color(color.r, color.g, color.b, color.a);
+ },
+ set: function (value) {
+ if ( !this._color.equals(value) ) {
+ var color = this._color;
+ color.r = value.r;
+ color.g = value.g;
+ color.b = value.b;
+ if (CC_DEV && value.a !== 255) {
+ cc.warnID(1626);
+ }
+ if (this._sizeProvider instanceof _ccsg.Node) {
+ this._sizeProvider.setColor(value);
+ }
+ }
+ },
+ },
+
//properties moved from base node end
},
@@ -2049,11 +2131,13 @@ if (CC_JSB) {
* node.setCascadeColorEnabled(true);
*/
-var SameNameGetSets = ['skewX', 'skewY', 'position', 'rotation', 'rotationX', 'rotationY', 'scale', 'scaleX', 'scaleY', ];
+var SameNameGetSets = ['skewX', 'skewY', 'position', 'rotation', 'rotationX', 'rotationY',
+ 'scale', 'scaleX', 'scaleY', 'opacity', 'color', ];
var DiffNameGetSets = {
x: ['getPositionX', 'setPositionX'],
y: ['getPositionY', 'setPositionY'],
+ cascadeOpacity: ['isCascadeOpacityEnabled', 'setCascadeOpacityEnabled'],
};
Misc.propertyDefine(Node, SameNameGetSets, DiffNameGetSets);
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 2700529e22..15b36224e2 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -366,87 +366,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* node.tag = 1001;
*/
- /**
- * !#en Opacity of node, default value is 255.
- * !#zh 节点透明度,默认值为 255。
- * @property opacity
- * @type {Number}
- * @example
- * node.opacity = 255;
- */
- opacity: {
- get: function () {
- return this._opacity;
- },
- set: function (value) {
- if (this._opacity !== value) {
- this._opacity = value;
- this._sgNode.setOpacity(value);
- if (!this._cascadeOpacityEnabled) {
- var sizeProvider = this._sizeProvider;
- if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
- sizeProvider.setOpacity(value);
- }
- }
- }
- },
- range: [0, 255]
- },
-
- /**
- * !#en Indicate whether node's opacity value affect its child nodes, default value is true.
- * !#zh 节点的不透明度值是否影响其子节点,默认值为 true。
- * @property cascadeOpacity
- * @type {Boolean}
- * @example
- * cc.log("CascadeOpacity: " + node.cascadeOpacity);
- */
- cascadeOpacity: {
- get: function () {
- return this._cascadeOpacityEnabled;
- },
- set: function (value) {
- if (this._cascadeOpacityEnabled !== value) {
- this._cascadeOpacityEnabled = value;
- this._sgNode.cascadeOpacity = value;
-
- var opacity = value ? 255 : this._opacity;
- var sizeProvider = this._sizeProvider;
- if (sizeProvider instanceof _ccsg.Node) {
- sizeProvider.setOpacity(opacity);
- }
- }
- },
- },
-
- /**
- * !#en Color of node, default value is white: (255, 255, 255).
- * !#zh 节点颜色。默认为白色,数值为:(255,255,255)。
- * @property color
- * @type {Color}
- * @example
- * node.color = new cc.Color(255, 255, 255);
- */
- color: {
- get: function () {
- var color = this._color;
- return new cc.Color(color.r, color.g, color.b, color.a);
- },
- set: function (value) {
- if ( !this._color.equals(value) ) {
- var color = this._color;
- color.r = value.r;
- color.g = value.g;
- color.b = value.b;
- if (CC_DEV && value.a !== 255) {
- cc.warnID(1626);
- }
- if (this._sizeProvider instanceof _ccsg.Node) {
- this._sizeProvider.setColor(value);
- }
- }
- },
- },
},
ctor: function () {
@@ -1714,11 +1633,10 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
// Define public getter and setter methods to ensure api compatibility.
var SameNameGetSets = ['name', 'children', 'childrenCount', 'parent',
- /*'shaderProgram',*/ 'opacity', 'color', 'tag'];
+ /*'shaderProgram',*/ 'tag'];
var DiffNameGetSets = {
zIndex: ['getLocalZOrder', 'setLocalZOrder'],
opacityModifyRGB: ['isOpacityModifyRGB'],
- cascadeOpacity: ['isCascadeOpacityEnabled', 'setCascadeOpacityEnabled'],
//// privates
//width: ['_getWidth', '_setWidth'],
//height: ['_getHeight', '_setHeight'],
From a555311e23c3454b3f27fb43f79f71200e94d14b Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 11:44:14 +0800
Subject: [PATCH 07/36] move opacityModifyRGB to CCNode
---
cocos2d/core/CCNode.js | 34 +++++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 34 ---------------------------------
2 files changed, 34 insertions(+), 34 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index fd2896c8c1..a5d2127e98 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -1931,6 +1931,39 @@ var Node = cc.Class({
}
},
+ /**
+ * !#en
+ * 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.
+ * !#zh 设置更改透明度时是否修改RGB值,
+ * @method setOpacityModifyRGB
+ * @param {Boolean} opacityValue
+ * @example
+ * node.setOpacityModifyRGB(true);
+ */
+ setOpacityModifyRGB: function (opacityValue) {
+ if (this._opacityModifyRGB !== opacityValue) {
+ this._opacityModifyRGB = opacityValue;
+ this._sgNode.setOpacityModifyRGB(opacityValue);
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
+ sizeProvider.setOpacityModifyRGB(opacityValue);
+ }
+ }
+ },
+
+ /**
+ * !#en Get whether color should be changed with the opacity value.
+ * !#zh 更改透明度时是否修改RGB值。
+ * @method isOpacityModifyRGB
+ * @return {Boolean}
+ * @example
+ * var hasChange = node.isOpacityModifyRGB();
+ */
+ isOpacityModifyRGB: function () {
+ return this._opacityModifyRGB;
+ },
+
//functions moved from base node end
});
@@ -2137,6 +2170,7 @@ var SameNameGetSets = ['skewX', 'skewY', 'position', 'rotation', 'rotationX', 'r
var DiffNameGetSets = {
x: ['getPositionX', 'setPositionX'],
y: ['getPositionY', 'setPositionY'],
+ opacityModifyRGB: ['isOpacityModifyRGB', 'setOpacityModifyRGB'],
cascadeOpacity: ['isCascadeOpacityEnabled', 'setCascadeOpacityEnabled'],
};
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 15b36224e2..602bf11fde 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -1353,39 +1353,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return this._sgNode.getDisplayedColor();
},
- /**
- * !#en
- * 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.
- * !#zh 设置更改透明度时是否修改RGB值,
- * @method setOpacityModifyRGB
- * @param {Boolean} opacityValue
- * @example
- * node.setOpacityModifyRGB(true);
- */
- setOpacityModifyRGB: function (opacityValue) {
- if (this._opacityModifyRGB !== opacityValue) {
- this._opacityModifyRGB = opacityValue;
- this._sgNode.setOpacityModifyRGB(opacityValue);
- var sizeProvider = this._sizeProvider;
- if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
- sizeProvider.setOpacityModifyRGB(opacityValue);
- }
- }
- },
-
- /**
- * !#en Get whether color should be changed with the opacity value.
- * !#zh 更改透明度时是否修改RGB值。
- * @method isOpacityModifyRGB
- * @return {Boolean}
- * @example
- * var hasChange = node.isOpacityModifyRGB();
- */
- isOpacityModifyRGB: function () {
- return this._opacityModifyRGB;
- },
-
// HIERARCHY METHODS
/**
@@ -1636,7 +1603,6 @@ var SameNameGetSets = ['name', 'children', 'childrenCount', 'parent',
/*'shaderProgram',*/ 'tag'];
var DiffNameGetSets = {
zIndex: ['getLocalZOrder', 'setLocalZOrder'],
- opacityModifyRGB: ['isOpacityModifyRGB'],
//// privates
//width: ['_getWidth', '_setWidth'],
//height: ['_getHeight', '_setHeight'],
From 9a6c4065713fb8a8d2721f2ae9b7a2cfac4bf5d3 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 11:58:55 +0800
Subject: [PATCH 08/36] move anchor/width/height/ignoreAnchor to CCNode
---
cocos2d/core/CCNode.js | 151 ++++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 151 --------------------------------
2 files changed, 151 insertions(+), 151 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index a5d2127e98..88ad45791b 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -786,6 +786,157 @@ var Node = cc.Class({
},
},
+ /**
+ * !#en Anchor point's position on x axis.
+ * !#zh 节点 X 轴锚点位置。
+ * @property anchorX
+ * @type {Number}
+ * @example
+ * node.anchorX = 0;
+ */
+ anchorX: {
+ get: function () {
+ return this._anchorPoint.x;
+ },
+ set: function (value) {
+ var anchorPoint = this._anchorPoint;
+ if (anchorPoint.x !== value) {
+ anchorPoint.x = value;
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider instanceof _ccsg.Node) {
+ sizeProvider.setAnchorPoint(anchorPoint);
+ }
+ this.emit(ANCHOR_CHANGED);
+ }
+ },
+ },
+
+ /**
+ * !#en Anchor point's position on y axis.
+ * !#zh 节点 Y 轴锚点位置。
+ * @property anchorY
+ * @type {Number}
+ * @example
+ * node.anchorY = 0;
+ */
+ anchorY: {
+ get: function () {
+ return this._anchorPoint.y;
+ },
+ set: function (value) {
+ var anchorPoint = this._anchorPoint;
+ if (anchorPoint.y !== value) {
+ anchorPoint.y = value;
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider instanceof _ccsg.Node) {
+ sizeProvider.setAnchorPoint(anchorPoint);
+ }
+ this.emit(ANCHOR_CHANGED);
+ }
+ },
+ },
+
+ /**
+ * !#en Width of node.
+ * !#zh 节点宽度。
+ * @property width
+ * @type {Number}
+ * @example
+ * node.width = 100;
+ */
+ width: {
+ get: function () {
+ if (this._sizeProvider) {
+ var w = this._sizeProvider._getWidth();
+ this._contentSize.width = w;
+ return w;
+ }
+ else {
+ return this._contentSize.width;
+ }
+ },
+ set: function (value) {
+ if (value !== this._contentSize.width) {
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider) {
+ sizeProvider.setContentSize(value, sizeProvider._getHeight());
+ }
+ if (CC_EDITOR) {
+ var clone = cc.size(this._contentSize);
+ }
+ this._contentSize.width = value;
+ if (CC_EDITOR) {
+ this.emit(SIZE_CHANGED, clone);
+ }
+ else {
+ this.emit(SIZE_CHANGED);
+ }
+ }
+ },
+ },
+
+ /**
+ * !#en Height of node.
+ * !#zh 节点高度。
+ * @property height
+ * @type {Number}
+ * @example
+ * node.height = 100;
+ */
+ height: {
+ get: function () {
+ if (this._sizeProvider) {
+ var h = this._sizeProvider._getHeight();
+ this._contentSize.height = h;
+ return h;
+ }
+ else {
+ return this._contentSize.height;
+ }
+ },
+ set: function (value) {
+ if (value !== this._contentSize.height) {
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider) {
+ sizeProvider.setContentSize(sizeProvider._getWidth(), value);
+ }
+ if (CC_EDITOR) {
+ var clone = cc.size(this._contentSize);
+ }
+ this._contentSize.height = value;
+ if (CC_EDITOR) {
+ this.emit(SIZE_CHANGED, clone);
+ }
+ else {
+ this.emit(SIZE_CHANGED);
+ }
+ }
+ },
+ },
+
+ /**
+ * Indicate whether ignore the anchor point property for positioning.
+ * @property _ignoreAnchor
+ * @type {Boolean}
+ * @private
+ */
+ _ignoreAnchor: {
+ get: function () {
+ return this.__ignoreAnchor;
+ },
+ set: function (value) {
+ if (this.__ignoreAnchor !== value) {
+ this.__ignoreAnchor = value;
+ this._sgNode.ignoreAnchor = value;
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
+ sizeProvider.ignoreAnchor = value;
+ }
+ this.emit(ANCHOR_CHANGED);
+ }
+ },
+ },
+
//properties moved from base node end
},
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 602bf11fde..66277da361 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -206,157 +206,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /**
- * !#en Anchor point's position on x axis.
- * !#zh 节点 X 轴锚点位置。
- * @property anchorX
- * @type {Number}
- * @example
- * node.anchorX = 0;
- */
- anchorX: {
- get: function () {
- return this._anchorPoint.x;
- },
- set: function (value) {
- var anchorPoint = this._anchorPoint;
- if (anchorPoint.x !== value) {
- anchorPoint.x = value;
- var sizeProvider = this._sizeProvider;
- if (sizeProvider instanceof _ccsg.Node) {
- sizeProvider.setAnchorPoint(anchorPoint);
- }
- this.emit(ANCHOR_CHANGED);
- }
- },
- },
-
- /**
- * !#en Anchor point's position on y axis.
- * !#zh 节点 Y 轴锚点位置。
- * @property anchorY
- * @type {Number}
- * @example
- * node.anchorY = 0;
- */
- anchorY: {
- get: function () {
- return this._anchorPoint.y;
- },
- set: function (value) {
- var anchorPoint = this._anchorPoint;
- if (anchorPoint.y !== value) {
- anchorPoint.y = value;
- var sizeProvider = this._sizeProvider;
- if (sizeProvider instanceof _ccsg.Node) {
- sizeProvider.setAnchorPoint(anchorPoint);
- }
- this.emit(ANCHOR_CHANGED);
- }
- },
- },
-
- /**
- * !#en Width of node.
- * !#zh 节点宽度。
- * @property width
- * @type {Number}
- * @example
- * node.width = 100;
- */
- width: {
- get: function () {
- if (this._sizeProvider) {
- var w = this._sizeProvider._getWidth();
- this._contentSize.width = w;
- return w;
- }
- else {
- return this._contentSize.width;
- }
- },
- set: function (value) {
- if (value !== this._contentSize.width) {
- var sizeProvider = this._sizeProvider;
- if (sizeProvider) {
- sizeProvider.setContentSize(value, sizeProvider._getHeight());
- }
- if (CC_EDITOR) {
- var clone = cc.size(this._contentSize);
- }
- this._contentSize.width = value;
- if (CC_EDITOR) {
- this.emit(SIZE_CHANGED, clone);
- }
- else {
- this.emit(SIZE_CHANGED);
- }
- }
- },
- },
-
- /**
- * !#en Height of node.
- * !#zh 节点高度。
- * @property height
- * @type {Number}
- * @example
- * node.height = 100;
- */
- height: {
- get: function () {
- if (this._sizeProvider) {
- var h = this._sizeProvider._getHeight();
- this._contentSize.height = h;
- return h;
- }
- else {
- return this._contentSize.height;
- }
- },
- set: function (value) {
- if (value !== this._contentSize.height) {
- var sizeProvider = this._sizeProvider;
- if (sizeProvider) {
- sizeProvider.setContentSize(sizeProvider._getWidth(), value);
- }
- if (CC_EDITOR) {
- var clone = cc.size(this._contentSize);
- }
- this._contentSize.height = value;
- if (CC_EDITOR) {
- this.emit(SIZE_CHANGED, clone);
- }
- else {
- this.emit(SIZE_CHANGED);
- }
- }
- },
- },
-
- /**
- * Indicate whether ignore the anchor point property for positioning.
- * @property _ignoreAnchor
- * @type {Boolean}
- * @private
- */
- _ignoreAnchor: {
- get: function () {
- return this.__ignoreAnchor;
- },
- set: function (value) {
- if (this.__ignoreAnchor !== value) {
- this.__ignoreAnchor = value;
- this._sgNode.ignoreAnchor = value;
- var sizeProvider = this._sizeProvider;
- if (sizeProvider instanceof _ccsg.Node && sizeProvider !== this._sgNode) {
- sizeProvider.ignoreAnchor = value;
- }
- this.emit(ANCHOR_CHANGED);
- }
- },
- },
-
/**
* !#en Tag of node.
* !#zh 节点标签。
From 4b1014e5c96c84b30a67b6aeec6d21c62dcf4f7b Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 13:51:11 +0800
Subject: [PATCH 09/36] move globalZorder anchor to CCNode
---
cocos2d/core/CCNode.js | 133 ++++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 133 --------------------------------
2 files changed, 133 insertions(+), 133 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 88ad45791b..897f999c43 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2115,6 +2115,139 @@ var Node = cc.Class({
return this._opacityModifyRGB;
},
+ /*
+ * !#en
+ * Defines the oder in which the nodes are renderer.
+ * Nodes that have a Global Z Order lower, are renderer first.
+ *
+ * In case two or more nodes have the same Global Z Order, the oder is not guaranteed.
+ * The only exception if the Nodes have a Global Z Order == 0. In that case, the Scene Graph order is used.
+ *
+ * By default, all nodes have a Global Z Order = 0. That means that by default, the Scene Graph order is used to render the nodes.
+ *
+ * Global Z Order is useful when you need to render nodes in an order different than the Scene Graph order.
+ *
+ * Limitations: Global Z Order can't be used used by Nodes that have SpriteBatchNode as one of their ancestors.
+ * And if ClippingNode is one of the ancestors, then "global Z order" will be relative to the ClippingNode.
+ * !#zh
+ * 定义节点的渲染顺序。
+ * 节点具有全局 Z 顺序,顺序越小的节点,最先渲染。
+ *
+ * 假设两个或者更多的节点拥有相同的全局 Z 顺序,那么渲染顺序无法保证。
+ * 唯一的例外是如果节点的全局 Z 顺序为零,那么场景中的顺序是可以使用默认的。
+ *
+ * 所有的节点全局 Z 顺序都是零。这就是说,默认使用场景中的顺序来渲染节点。
+ *
+ * 全局 Z 顺序是非常有用的当你需要渲染节点按照不同的顺序而不是场景顺序。
+ *
+ * 局限性: 全局 Z 顺序不能够被拥有继承 “SpriteBatchNode” 的节点使用。
+ * 并且如果 “ClippingNode” 是其中之一的上代,那么 “global Z order” 将会和 “ClippingNode” 有关。
+ * @method setGlobalZOrder
+ * @param {Number} globalZOrder
+ * @example
+ * node.setGlobalZOrder(0);
+ */
+ setGlobalZOrder: function (globalZOrder) {
+ this._globalZOrder = globalZOrder;
+ this._sgNode.setGlobalZOrder(globalZOrder);
+ },
+
+ /*
+ * !#en Return the Node's Global Z Order.
+ * !#zh 获取节点的全局 Z 顺序。
+ * @method getGlobalZOrder
+ * @returns {number} The node's global Z order
+ * @example
+ * cc.log("Global Z Order: " + node.getGlobalZOrder());
+ */
+ getGlobalZOrder: function () {
+ this._globalZOrder = this._sgNode.getGlobalZOrder();
+ return this._globalZOrder;
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 获取节点锚点,用百分比表示。
+ * 锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
+ * 锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
+ * 但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
+ * 默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
+ * 注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。
+ * @method getAnchorPoint
+ * @return {Vec2} The anchor point of node.
+ * @example
+ * cc.log("Node AnchorPoint: " + node.getAnchorPoint());
+ */
+ getAnchorPoint: function () {
+ return cc.p(this._anchorPoint);
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 设置锚点的百分比。
+ * 锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
+ * 锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
+ * 但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
+ * 默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
+ * 注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。
+ * @method setAnchorPoint
+ * @param {Vec2|Number} point - The anchor point of node or The x axis anchor of node.
+ * @param {Number} [y] - The y axis anchor of node.
+ * @example
+ * node.setAnchorPoint(cc.v2(1, 1));
+ * node.setAnchorPoint(1, 1);
+ */
+ setAnchorPoint: function (point, y) {
+ var locAnchorPoint = this._anchorPoint;
+ if (y === undefined) {
+ if ((point.x === locAnchorPoint.x) && (point.y === locAnchorPoint.y))
+ return;
+ locAnchorPoint.x = point.x;
+ locAnchorPoint.y = point.y;
+ } else {
+ if ((point === locAnchorPoint.x) && (y === locAnchorPoint.y))
+ return;
+ locAnchorPoint.x = point;
+ locAnchorPoint.y = y;
+ }
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider instanceof _ccsg.Node) {
+ sizeProvider.setAnchorPoint(locAnchorPoint);
+ }
+ this.emit(ANCHOR_CHANGED);
+ },
+
+ /**
+ * !#en
+ * Returns a copy of the anchor point in absolute pixels.
+ * you can only read it. If you wish to modify it, use setAnchorPoint.
+ * !#zh
+ * 返回锚点的绝对像素位置。
+ * 你只能读它。如果您要修改它,使用 setAnchorPoint。
+ * @see cc.Node#getAnchorPoint
+ * @method getAnchorPointInPoints
+ * @return {Vec2} The anchor point in absolute pixels.
+ * @example
+ * cc.log("AnchorPointInPoints: " + node.getAnchorPointInPoints());
+ */
+ getAnchorPointInPoints: function () {
+ return this._sgNode.getAnchorPointInPoints();
+ },
+
//functions moved from base node end
});
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 66277da361..5e53114e3f 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -367,139 +367,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /*
- * !#en
- * Defines the oder in which the nodes are renderer.
- * Nodes that have a Global Z Order lower, are renderer first.
- *
- * In case two or more nodes have the same Global Z Order, the oder is not guaranteed.
- * The only exception if the Nodes have a Global Z Order == 0. In that case, the Scene Graph order is used.
- *
- * By default, all nodes have a Global Z Order = 0. That means that by default, the Scene Graph order is used to render the nodes.
- *
- * Global Z Order is useful when you need to render nodes in an order different than the Scene Graph order.
- *
- * Limitations: Global Z Order can't be used used by Nodes that have SpriteBatchNode as one of their ancestors.
- * And if ClippingNode is one of the ancestors, then "global Z order" will be relative to the ClippingNode.
- * !#zh
- * 定义节点的渲染顺序。
- * 节点具有全局 Z 顺序,顺序越小的节点,最先渲染。
- *
- * 假设两个或者更多的节点拥有相同的全局 Z 顺序,那么渲染顺序无法保证。
- * 唯一的例外是如果节点的全局 Z 顺序为零,那么场景中的顺序是可以使用默认的。
- *
- * 所有的节点全局 Z 顺序都是零。这就是说,默认使用场景中的顺序来渲染节点。
- *
- * 全局 Z 顺序是非常有用的当你需要渲染节点按照不同的顺序而不是场景顺序。
- *
- * 局限性: 全局 Z 顺序不能够被拥有继承 “SpriteBatchNode” 的节点使用。
- * 并且如果 “ClippingNode” 是其中之一的上代,那么 “global Z order” 将会和 “ClippingNode” 有关。
- * @method setGlobalZOrder
- * @param {Number} globalZOrder
- * @example
- * node.setGlobalZOrder(0);
- */
- setGlobalZOrder: function (globalZOrder) {
- this._globalZOrder = globalZOrder;
- this._sgNode.setGlobalZOrder(globalZOrder);
- },
-
- /*
- * !#en Return the Node's Global Z Order.
- * !#zh 获取节点的全局 Z 顺序。
- * @method getGlobalZOrder
- * @returns {number} The node's global Z order
- * @example
- * cc.log("Global Z Order: " + node.getGlobalZOrder());
- */
- getGlobalZOrder: function () {
- this._globalZOrder = this._sgNode.getGlobalZOrder();
- return this._globalZOrder;
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 获取节点锚点,用百分比表示。
- * 锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
- * 锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
- * 但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
- * 默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
- * 注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。
- * @method getAnchorPoint
- * @return {Vec2} The anchor point of node.
- * @example
- * cc.log("Node AnchorPoint: " + node.getAnchorPoint());
- */
- getAnchorPoint: function () {
- return cc.p(this._anchorPoint);
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 设置锚点的百分比。
- * 锚点应用于所有变换和坐标点的操作,它就像在节点上连接其父节点的大头针。
- * 锚点是标准化的,就像百分比一样。(0,0) 表示左下角,(1,1) 表示右上角。
- * 但是你可以使用比(1,1)更高的值或者比(0,0)更低的值。
- * 默认的锚点是(0.5,0.5),因此它开始于节点的中心位置。
- * 注意:Creator 中的锚点仅用于定位所在的节点,子节点的定位不受影响。
- * @method setAnchorPoint
- * @param {Vec2|Number} point - The anchor point of node or The x axis anchor of node.
- * @param {Number} [y] - The y axis anchor of node.
- * @example
- * node.setAnchorPoint(cc.v2(1, 1));
- * node.setAnchorPoint(1, 1);
- */
- setAnchorPoint: function (point, y) {
- var locAnchorPoint = this._anchorPoint;
- if (y === undefined) {
- if ((point.x === locAnchorPoint.x) && (point.y === locAnchorPoint.y))
- return;
- locAnchorPoint.x = point.x;
- locAnchorPoint.y = point.y;
- } else {
- if ((point === locAnchorPoint.x) && (y === locAnchorPoint.y))
- return;
- locAnchorPoint.x = point;
- locAnchorPoint.y = y;
- }
- var sizeProvider = this._sizeProvider;
- if (sizeProvider instanceof _ccsg.Node) {
- sizeProvider.setAnchorPoint(locAnchorPoint);
- }
- this.emit(ANCHOR_CHANGED);
- },
-
- /**
- * !#en
- * Returns a copy of the anchor point in absolute pixels.
- * you can only read it. If you wish to modify it, use setAnchorPoint.
- * !#zh
- * 返回锚点的绝对像素位置。
- * 你只能读它。如果您要修改它,使用 setAnchorPoint。
- * @see cc.Node#getAnchorPoint
- * @method getAnchorPointInPoints
- * @return {Vec2} The anchor point in absolute pixels.
- * @example
- * cc.log("AnchorPointInPoints: " + node.getAnchorPointInPoints());
- */
- getAnchorPointInPoints: function () {
- return this._sgNode.getAnchorPointInPoints();
- },
-
/**
* !#en
* Returns a copy the untransformed size of the node.
From d6c7a8de92825f925b1b544a9451c498fd38df13 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 14:05:42 +0800
Subject: [PATCH 10/36] refactor: expand updateOrder function in base node
---
cocos2d/core/utils/base-node.js | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 5e53114e3f..2be83f4dd4 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -28,14 +28,6 @@ var Destroying = require('../platform/CCObject').Flags.Destroying;
var Misc = require('./misc');
var IdGenerater = require('../platform/id-generater');
-function updateOrder (node) {
- var parent = node._parent;
- parent._delaySort();
- if (!CC_JSB) {
- cc.eventManager._setDirtyForNode(node);
- }
-}
-
var POSITION_CHANGED = 'position-changed';
var SIZE_CHANGED = 'size-changed';
var ANCHOR_CHANGED = 'anchor-changed';
@@ -166,7 +158,10 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
this._sgNode.zIndex = value;
if(this._parent) {
- updateOrder(this);
+ this._parent._delaySort();
+ if (!CC_JSB) {
+ cc.eventManager._setDirtyForNode(this);
+ }
}
}
}
@@ -300,7 +295,10 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
if (value) {
var parent = value._sgNode;
parent.addChild(sgNode);
- updateOrder(this);
+ value._delaySort();
+ if (!CC_JSB) {
+ cc.eventManager._setDirtyForNode(this);
+ }
value._children.push(this);
value.emit(CHILD_ADDED, this);
}
From 59df3d9a2760ebe20a3bc929c6433226ed7f1985 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 14:07:23 +0800
Subject: [PATCH 11/36] move zIndex to CCNode
---
cocos2d/core/CCNode.js | 29 +++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 29 -----------------------------
2 files changed, 29 insertions(+), 29 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 897f999c43..c25e44c858 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -937,6 +937,34 @@ var Node = cc.Class({
},
},
+ /**
+ * !#en Z order in depth which stands for the drawing order.
+ * !#zh 该节点渲染排序的 Z 轴深度。
+ * @property zIndex
+ * @type {Number}
+ * @example
+ * node.zIndex = 1;
+ * cc.log("Node zIndex: " + node.zIndex);
+ */
+ zIndex: {
+ get: function () {
+ return this._localZOrder;
+ },
+ set: function (value) {
+ if (this._localZOrder !== value) {
+ this._localZOrder = value;
+ this._sgNode.zIndex = value;
+
+ if(this._parent) {
+ this._parent._delaySort();
+ if (!CC_JSB) {
+ cc.eventManager._setDirtyForNode(this);
+ }
+ }
+ }
+ }
+ },
+
//properties moved from base node end
},
@@ -2454,6 +2482,7 @@ var SameNameGetSets = ['skewX', 'skewY', 'position', 'rotation', 'rotationX', 'r
var DiffNameGetSets = {
x: ['getPositionX', 'setPositionX'],
y: ['getPositionY', 'setPositionY'],
+ zIndex: ['getLocalZOrder', 'setLocalZOrder'],
opacityModifyRGB: ['isOpacityModifyRGB', 'setOpacityModifyRGB'],
cascadeOpacity: ['isCascadeOpacityEnabled', 'setCascadeOpacityEnabled'],
};
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 2be83f4dd4..627b12d0b0 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -139,34 +139,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /**
- * !#en Z order in depth which stands for the drawing order.
- * !#zh 该节点渲染排序的 Z 轴深度。
- * @property zIndex
- * @type {Number}
- * @example
- * node.zIndex = 1;
- * cc.log("Node zIndex: " + node.zIndex);
- */
- zIndex: {
- get: function () {
- return this._localZOrder;
- },
- set: function (value) {
- if (this._localZOrder !== value) {
- this._localZOrder = value;
- this._sgNode.zIndex = value;
-
- if(this._parent) {
- this._parent._delaySort();
- if (!CC_JSB) {
- cc.eventManager._setDirtyForNode(this);
- }
- }
- }
- }
- },
-
/**
* !#en All children nodes.
* !#zh 节点的所有子节点。
@@ -1316,7 +1288,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
var SameNameGetSets = ['name', 'children', 'childrenCount', 'parent',
/*'shaderProgram',*/ 'tag'];
var DiffNameGetSets = {
- zIndex: ['getLocalZOrder', 'setLocalZOrder'],
//// privates
//width: ['_getWidth', '_setWidth'],
//height: ['_getHeight', '_setHeight'],
From 09def3a2710d0c60fd3ba4e3b7301ba3d3f4a592 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 14:24:38 +0800
Subject: [PATCH 12/36] move
getDisplayedOpacity/_updateDisplayedOpacity/getDisplayedColor to CCNode
---
cocos2d/core/CCNode.js | 46 +++++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 46 ---------------------------------
2 files changed, 46 insertions(+), 46 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index c25e44c858..65d9f94520 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2276,6 +2276,52 @@ var Node = cc.Class({
return this._sgNode.getAnchorPointInPoints();
},
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 获取节点显示透明度,
+ * 显示透明度和透明度之间的不同之处在于当启用级连透明度时,
+ * 显示透明度是基于自身透明度和父节点透明度计算的。
+ *
+ * @method getDisplayedOpacity
+ * @returns {number} displayed opacity
+ * @example
+ * var displayOpacity = node.getDisplayedOpacity();
+ */
+ getDisplayedOpacity: function () {
+ return this._sgNode.getDisplayedOpacity();
+ },
+
+ /*
+ * !#en Update displayed opacity.
+ * !#zh 更新显示透明度。
+ * @method _updateDisplayedOpacity
+ * @param {Number} parentOpacity
+ * @example
+ * node._updateDisplayedOpacity(255);
+ */
+ _updateDisplayedOpacity: function (parentOpacity) {
+ this._sgNode.updateDisplayedOpacity(parentOpacity);
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 获取节点的显示透明度,
+ * 显示透明度和透明度之间的不同之处在于显示透明度是基于透明度和父节点透明度启用级连透明度时计算的。
+ * @method getDisplayedColor
+ * @returns {Color}
+ * @example
+ * var displayColor = node.getDisplayedColor();
+ */
+ getDisplayedColor: function () {
+ return this._sgNode.getDisplayedColor();
+ },
+
//functions moved from base node end
});
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 627b12d0b0..853d4bad7d 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -993,52 +993,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return rect;
},
- /**
- * !#en
- * 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.
- * !#zh
- * 获取节点显示透明度,
- * 显示透明度和透明度之间的不同之处在于当启用级连透明度时,
- * 显示透明度是基于自身透明度和父节点透明度计算的。
- *
- * @method getDisplayedOpacity
- * @returns {number} displayed opacity
- * @example
- * var displayOpacity = node.getDisplayedOpacity();
- */
- getDisplayedOpacity: function () {
- return this._sgNode.getDisplayedOpacity();
- },
-
- /*
- * !#en Update displayed opacity.
- * !#zh 更新显示透明度。
- * @method _updateDisplayedOpacity
- * @param {Number} parentOpacity
- * @example
- * node._updateDisplayedOpacity(255);
- */
- _updateDisplayedOpacity: function (parentOpacity) {
- this._sgNode.updateDisplayedOpacity(parentOpacity);
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 获取节点的显示透明度,
- * 显示透明度和透明度之间的不同之处在于显示透明度是基于透明度和父节点透明度启用级连透明度时计算的。
- * @method getDisplayedColor
- * @returns {Color}
- * @example
- * var displayColor = node.getDisplayedColor();
- */
- getDisplayedColor: function () {
- return this._sgNode.getDisplayedColor();
- },
-
// HIERARCHY METHODS
/**
From 635e74f0704026657148c86e8e6227986d7c1c43 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Fri, 23 Dec 2016 14:44:01 +0800
Subject: [PATCH 13/36] =?UTF-8?q?move=20getNodeToParentTransformAR?=
=?UTF-8?q?=EF=BC=8FgetBoundingBox=EF=BC=8FgetBoundingBoxToWorld=EF=BC=8F?=
=?UTF-8?q?=5FgetBoun=20dingBoxTo=20to=20CCNode?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
cocos2d/core/CCNode.js | 88 +++++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 86 --------------------------------
2 files changed, 88 insertions(+), 86 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 65d9f94520..2c88bfb5a5 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2322,6 +2322,94 @@ var Node = cc.Class({
return this._sgNode.getDisplayedColor();
},
+ /**
+ * !#en
+ * 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).
+ * !#zh
+ * 返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。
+ * 这个矩阵以像素为单位。
+ * 该方法基于节点坐标。
+ * @method getNodeToParentTransformAR
+ * @return {AffineTransform} The affine transform object
+ * @example
+ * var affineTransform = node.getNodeToParentTransformAR();
+ */
+ getNodeToParentTransformAR: function () {
+ var contentSize = this.getContentSize();
+ var mat = this._sgNode.getNodeToParentTransform();
+ if ( !this._isSgTransformArToMe(contentSize) ) {
+ // see getNodeToWorldTransform
+ var tx = this._anchorPoint.x * contentSize.width;
+ var ty = this._anchorPoint.y * contentSize.height;
+ var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
+ mat = cc.affineTransformConcatIn(offset, mat);
+ }
+ return mat;
+ },
+
+ /**
+ * !#en
+ * Returns a "local" axis aligned bounding box of the node.
+ * The returned box is relative only to its parent.
+ * !#zh 返回父节坐标系下的轴向对齐的包围盒。
+ * @method getBoundingBox
+ * @return {Rect} The calculated bounding box of the node
+ * @example
+ * var boundingBox = node.getBoundingBox();
+ */
+ getBoundingBox: function () {
+ var size = this.getContentSize();
+ var rect = cc.rect( 0, 0, size.width, size.height );
+ return cc._rectApplyAffineTransformIn(rect, this.getNodeToParentTransform());
+ },
+
+ /**
+ * !#en
+ * Returns a "world" axis aligned bounding box of the node.
+ * The bounding box contains self and active children's world bounding box.
+ * !#zh
+ * 返回节点在世界坐标系下的对齐轴向的包围盒(AABB)。
+ * 该边框包含自身和已激活的子节点的世界边框。
+ * @method getBoundingBoxToWorld
+ * @return {Rect}
+ * @example
+ * var newRect = node.getBoundingBoxToWorld();
+ */
+ getBoundingBoxToWorld: function () {
+ var trans;
+ if (this.parent) {
+ trans = this.parent.getNodeToWorldTransformAR();
+ }
+ return this._getBoundingBoxTo(trans);
+ },
+
+ _getBoundingBoxTo: function (parentTransformAR) {
+ var size = this.getContentSize();
+ var width = size.width;
+ var height = size.height;
+ var rect = cc.rect(- this._anchorPoint.x * width, - this._anchorPoint.y * height, width, height);
+
+ var transAR = cc.affineTransformConcat(this.getNodeToParentTransformAR(), parentTransformAR);
+ cc._rectApplyAffineTransformIn(rect, transAR);
+
+ //query child's BoundingBox
+ if (!this._children)
+ return rect;
+
+ var locChildren = this._children;
+ for (var i = 0; i < locChildren.length; i++) {
+ var child = locChildren[i];
+ if (child && child.active) {
+ var childRect = child._getBoundingBoxTo(transAR);
+ if (childRect)
+ rect = cc.rectUnion(rect, childRect);
+ }
+ }
+ return rect;
+ },
+
//functions moved from base node end
});
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 853d4bad7d..79a42c99fb 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -404,21 +404,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /**
- * !#en
- * Returns a "local" axis aligned bounding box of the node.
- * The returned box is relative only to its parent.
- * !#zh 返回父节坐标系下的轴向对齐的包围盒。
- * @method getBoundingBox
- * @return {Rect} The calculated bounding box of the node
- * @example
- * var boundingBox = node.getBoundingBox();
- */
- getBoundingBox: function () {
- var size = this.getContentSize();
- var rect = cc.rect( 0, 0, size.width, size.height );
- return cc._rectApplyAffineTransformIn(rect, this.getNodeToParentTransform());
- },
/**
* !#en Stops all running actions and schedulers.
@@ -921,77 +906,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return mat;
},
- /**
- * !#en
- * 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).
- * !#zh
- * 返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。
- * 这个矩阵以像素为单位。
- * 该方法基于节点坐标。
- * @method getNodeToParentTransformAR
- * @return {AffineTransform} The affine transform object
- * @example
- * var affineTransform = node.getNodeToParentTransformAR();
- */
- getNodeToParentTransformAR: function () {
- var contentSize = this.getContentSize();
- var mat = this._sgNode.getNodeToParentTransform();
- if ( !this._isSgTransformArToMe(contentSize) ) {
- // see getNodeToWorldTransform
- var tx = this._anchorPoint.x * contentSize.width;
- var ty = this._anchorPoint.y * contentSize.height;
- var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
- mat = cc.affineTransformConcatIn(offset, mat);
- }
- return mat;
- },
-
- /**
- * !#en
- * Returns a "world" axis aligned bounding box of the node.
- * The bounding box contains self and active children's world bounding box.
- * !#zh
- * 返回节点在世界坐标系下的对齐轴向的包围盒(AABB)。
- * 该边框包含自身和已激活的子节点的世界边框。
- * @method getBoundingBoxToWorld
- * @return {Rect}
- * @example
- * var newRect = node.getBoundingBoxToWorld();
- */
- getBoundingBoxToWorld: function () {
- var trans;
- if (this.parent) {
- trans = this.parent.getNodeToWorldTransformAR();
- }
- return this._getBoundingBoxTo(trans);
- },
-
- _getBoundingBoxTo: function (parentTransformAR) {
- var size = this.getContentSize();
- var width = size.width;
- var height = size.height;
- var rect = cc.rect(- this._anchorPoint.x * width, - this._anchorPoint.y * height, width, height);
-
- var transAR = cc.affineTransformConcat(this.getNodeToParentTransformAR(), parentTransformAR);
- cc._rectApplyAffineTransformIn(rect, transAR);
-
- //query child's BoundingBox
- if (!this._children)
- return rect;
-
- var locChildren = this._children;
- for (var i = 0; i < locChildren.length; i++) {
- var child = locChildren[i];
- if (child && child.active) {
- var childRect = child._getBoundingBoxTo(transAR);
- if (childRect)
- rect = cc.rectUnion(rect, childRect);
- }
- }
- return rect;
- },
// HIERARCHY METHODS
From a2623c7146239a068b01efca5059bb23ce11c8df Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 10:08:35 +0800
Subject: [PATCH 14/36] setParent/setTag/_onPreDestroy implementation move to
CCNode,
---
cocos2d/core/CCNode.js | 50 ++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 63 +++------------------------------
2 files changed, 55 insertions(+), 58 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 2c88bfb5a5..5321285e3e 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -1990,6 +1990,56 @@ var Node = cc.Class({
//functions moved from base node begin
+ //override
+ setTag: function (value) {
+ this._tag = value;
+ this._sgNode.tag = value;
+ },
+
+ setParent: function (value) {
+ if (this._parent === value) {
+ return;
+ }
+ if (CC_EDITOR && !cc.engine.isPlaying) {
+ if (_Scene.DetectConflict.beforeAddChild(this)) {
+ return;
+ }
+ }
+ var sgNode = this._sgNode;
+ if (sgNode.parent) {
+ sgNode.parent.removeChild(sgNode, false);
+ }
+ //
+ var oldParent = this._parent;
+ this._parent = value || null;
+ if (value) {
+ var parent = value._sgNode;
+ parent.addChild(sgNode);
+ value._delaySort();
+ if (!CC_JSB) {
+ cc.eventManager._setDirtyForNode(this);
+ }
+ value._children.push(this);
+ value.emit(CHILD_ADDED, this);
+ }
+ if (oldParent) {
+ if (!(oldParent._objFlags & Destroying)) {
+ var removeAt = oldParent._children.indexOf(this);
+ if (CC_DEV && removeAt < 0) {
+ return cc.errorID(1633);
+ }
+ oldParent._children.splice(removeAt, 1);
+ oldParent.emit(CHILD_REMOVED, this);
+ this._onHierarchyChanged(oldParent);
+ }
+ }
+ else if (value) {
+ this._onHierarchyChanged(null);
+ }
+ },
+
+ //end override
+
/**
* !#en Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.
* !#zh 获取在父节点坐标系中节点的位置( x , y )。
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 79a42c99fb..a847371913 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -239,75 +239,22 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return this._tag;
},
- setTag: function (value) {
- this._tag = value;
- this._sgNode.tag = value;
- },
getParent: function() {
return this._parent;
},
- setParent: function (value) {
- if (this._parent === value) {
- return;
- }
- if (CC_EDITOR && !cc.engine.isPlaying) {
- if (_Scene.DetectConflict.beforeAddChild(this)) {
- return;
- }
- }
- var sgNode = this._sgNode;
- if (sgNode.parent) {
- sgNode.parent.removeChild(sgNode, false);
- }
- //
- var oldParent = this._parent;
- this._parent = value || null;
- if (value) {
- var parent = value._sgNode;
- parent.addChild(sgNode);
- value._delaySort();
- if (!CC_JSB) {
- cc.eventManager._setDirtyForNode(this);
- }
- value._children.push(this);
- value.emit(CHILD_ADDED, this);
- }
- if (oldParent) {
- if (!(oldParent._objFlags & Destroying)) {
- var removeAt = oldParent._children.indexOf(this);
- if (CC_DEV && removeAt < 0) {
- return cc.errorID(1633);
- }
- oldParent._children.splice(removeAt, 1);
- oldParent.emit(CHILD_REMOVED, this);
- this._onHierarchyChanged(oldParent);
- }
- }
- else if (value) {
- this._onHierarchyChanged(null);
- }
- },
+ //interface need to be implemented in derived classes
+ setTag: null,
+ setParent: null,
- _onPreDestroy: function () {
- cc.eventManager.removeListeners(this);
- if (CC_JSB) {
- this._sgNode.release();
- this._sgNode._entity = null;
- this._sgNode = null;
- }
- for (var i = 0, len = this.__eventTargets.length; i < len; ++i) {
- var target = this.__eventTargets[i];
- target && target.targetOff(this);
- }
- },
// ABSTRACT INTERFACES
// called when the node's parent changed
_onHierarchyChanged: null,
+ _onPreDestroy: null,
/*
* Initializes the instance of cc.Node
* @method init
@@ -443,7 +390,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
if (children !== null) {
for (var i = 0; i < children.length; i++) {
var node = children[i];
- if (node && node.tag === aTag)
+ if (node && node._tag === aTag)
return node;
}
}
From a2ae914e25ccbdff8415536adf80e85b94248a03 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 10:25:53 +0800
Subject: [PATCH 15/36] move parent/tag properties to CCNode
---
cocos2d/core/CCNode.js | 2 +-
cocos2d/core/utils/base-node.js | 3 +--
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 5321285e3e..6218bcff54 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2660,7 +2660,7 @@ if (CC_JSB) {
* node.setCascadeColorEnabled(true);
*/
-var SameNameGetSets = ['skewX', 'skewY', 'position', 'rotation', 'rotationX', 'rotationY',
+var SameNameGetSets = ['parent', 'tag', 'skewX', 'skewY', 'position', 'rotation', 'rotationX', 'rotationY',
'scale', 'scaleX', 'scaleY', 'opacity', 'color', ];
var DiffNameGetSets = {
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index a847371913..19076edb6e 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -1100,8 +1100,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
// Define public getter and setter methods to ensure api compatibility.
-var SameNameGetSets = ['name', 'children', 'childrenCount', 'parent',
- /*'shaderProgram',*/ 'tag'];
+var SameNameGetSets = ['name', 'children', 'childrenCount',];
var DiffNameGetSets = {
//// privates
//width: ['_getWidth', '_setWidth'],
From 28301c347dd38eae52131934cd0d74112c7c03e6 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 10:35:07 +0800
Subject: [PATCH 16/36] move convertToXXXSpaceXXX functions to CCNode
---
cocos2d/core/CCNode.js | 118 ++++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 117 -------------------------------
2 files changed, 118 insertions(+), 117 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 6218bcff54..5a61dfb18e 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2460,6 +2460,124 @@ var Node = cc.Class({
return rect;
},
+
+ /**
+ * !#en Converts a Point to node (local) space coordinates. The result is in Vec2.
+ * !#zh 将一个点转换到节点 (局部) 坐标系。结果以 Vec2 为单位。
+ * @method convertToNodeSpace
+ * @param {Vec2} worldPoint
+ * @return {Vec2}
+ * @example
+ * var newVec2 = node.convertToNodeSpace(cc.v2(100, 100));
+ */
+ convertToNodeSpace: function (worldPoint) {
+ if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ // ensure transform computed
+ cc.director._visitScene();
+ }
+ var nodePositionIgnoreAnchorPoint = this._sgNode.convertToNodeSpace(worldPoint);
+ return cc.pAdd(nodePositionIgnoreAnchorPoint, cc.p(this._anchorPoint.x * this._contentSize.width, this._anchorPoint.y * this._contentSize.height));
+ },
+
+ /**
+ * !#en Converts a Point to world space coordinates. The result is in Points.
+ * !#zh 将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
+ * @method convertToWorldSpace
+ * @param {Vec2} nodePoint
+ * @return {Vec2}
+ * @example
+ * var newVec2 = node.convertToWorldSpace(cc.v2(100, 100));
+ */
+ convertToWorldSpace: function (nodePoint) {
+ if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ // ensure transform computed
+ cc.director._visitScene();
+ }
+ var x = nodePoint.x - this._anchorPoint.x * this._contentSize.width;
+ var y = nodePoint.y - this._anchorPoint.y * this._contentSize.height;
+ return cc.v2(this._sgNode.convertToWorldSpace(cc.v2(x, y)));
+ },
+
+ /**
+ * !#en
+ * Converts a Point to node (local) space coordinates. The result is in Points.
+ * treating the returned/received node point as anchor relative.
+ * !#zh
+ * 将一个点转换到节点 (局部) 空间坐标系。结果以 Vec2 为单位。
+ * 返回值将基于节点坐标。
+ * @method convertToNodeSpaceAR
+ * @param {Vec2} worldPoint
+ * @return {Vec2}
+ * @example
+ * var newVec2 = node.convertToNodeSpaceAR(cc.v2(100, 100));
+ */
+ convertToNodeSpaceAR: function (worldPoint) {
+ if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ // ensure transform computed
+ cc.director._visitScene();
+ }
+ if (this._sgNode.isIgnoreAnchorPointForPosition()) {
+ // see https://github.com/cocos-creator/engine/pull/391
+ return cc.v2(this._sgNode.convertToNodeSpace(worldPoint));
+ }
+ else {
+ return this._sgNode.convertToNodeSpaceAR(worldPoint);
+ }
+ },
+
+ /**
+ * !#en
+ * Converts a local Point to world space coordinates.The result is in Points.
+ * treating the returned/received node point as anchor relative.
+ * !#zh
+ * 将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
+ * 返回值将基于世界坐标。
+ * @method convertToWorldSpaceAR
+ * @param {Vec2} nodePoint
+ * @return {Vec2}
+ * @example
+ * var newVec2 = node.convertToWorldSpaceAR(cc.v2(100, 100));
+ */
+ convertToWorldSpaceAR: function (nodePoint) {
+ if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ // ensure transform computed
+ cc.director._visitScene();
+ }
+ if (this._sgNode.isIgnoreAnchorPointForPosition()) {
+ // see https://github.com/cocos-creator/engine/pull/391
+ return cc.v2(this._sgNode.convertToWorldSpace(nodePoint));
+ }
+ else {
+ return cc.v2(this._sgNode.convertToWorldSpaceAR(nodePoint));
+ }
+ },
+
+ /**
+ * !#en convenience methods which take a cc.Touch instead of cc.Vec2.
+ * !#zh 将触摸点转换成本地坐标系中位置。
+ * @method convertTouchToNodeSpace
+ * @param {Touch} touch - The touch object
+ * @return {Vec2}
+ * @example
+ * var newVec2 = node.convertTouchToNodeSpace(touch);
+ */
+ convertTouchToNodeSpace: function (touch) {
+ return this.convertToNodeSpace(touch.getLocation());
+ },
+
+ /**
+ * !#en converts a cc.Touch (world coordinates) into a local coordinate. This method is AR (Anchor Relative).
+ * !#zh 转换一个 cc.Touch(世界坐标)到一个局部坐标,该方法基于节点坐标。
+ * @method convertTouchToNodeSpaceAR
+ * @param {Touch} touch - The touch object
+ * @return {Vec2}
+ * @example
+ * var newVec2 = node.convertTouchToNodeSpaceAR(touch);
+ */
+ convertTouchToNodeSpaceAR: function (touch) {
+ return this.convertToNodeSpaceAR(touch.getLocation());
+ },
+
//functions moved from base node end
});
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 19076edb6e..d5a7c89dba 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -713,123 +713,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return this._sgNode.getWorldToNodeTransform();
},
- /**
- * !#en Converts a Point to node (local) space coordinates. The result is in Vec2.
- * !#zh 将一个点转换到节点 (局部) 坐标系。结果以 Vec2 为单位。
- * @method convertToNodeSpace
- * @param {Vec2} worldPoint
- * @return {Vec2}
- * @example
- * var newVec2 = node.convertToNodeSpace(cc.v2(100, 100));
- */
- convertToNodeSpace: function (worldPoint) {
- if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
- // ensure transform computed
- cc.director._visitScene();
- }
- var nodePositionIgnoreAnchorPoint = this._sgNode.convertToNodeSpace(worldPoint);
- return cc.pAdd(nodePositionIgnoreAnchorPoint, cc.p(this._anchorPoint.x * this._contentSize.width, this._anchorPoint.y * this._contentSize.height));
- },
-
- /**
- * !#en Converts a Point to world space coordinates. The result is in Points.
- * !#zh 将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
- * @method convertToWorldSpace
- * @param {Vec2} nodePoint
- * @return {Vec2}
- * @example
- * var newVec2 = node.convertToWorldSpace(cc.v2(100, 100));
- */
- convertToWorldSpace: function (nodePoint) {
- if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
- // ensure transform computed
- cc.director._visitScene();
- }
- var x = nodePoint.x - this._anchorPoint.x * this._contentSize.width;
- var y = nodePoint.y - this._anchorPoint.y * this._contentSize.height;
- return cc.v2(this._sgNode.convertToWorldSpace(cc.v2(x, y)));
- },
-
- /**
- * !#en
- * Converts a Point to node (local) space coordinates. The result is in Points.
- * treating the returned/received node point as anchor relative.
- * !#zh
- * 将一个点转换到节点 (局部) 空间坐标系。结果以 Vec2 为单位。
- * 返回值将基于节点坐标。
- * @method convertToNodeSpaceAR
- * @param {Vec2} worldPoint
- * @return {Vec2}
- * @example
- * var newVec2 = node.convertToNodeSpaceAR(cc.v2(100, 100));
- */
- convertToNodeSpaceAR: function (worldPoint) {
- if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
- // ensure transform computed
- cc.director._visitScene();
- }
- if (this._sgNode.isIgnoreAnchorPointForPosition()) {
- // see https://github.com/cocos-creator/engine/pull/391
- return cc.v2(this._sgNode.convertToNodeSpace(worldPoint));
- }
- else {
- return this._sgNode.convertToNodeSpaceAR(worldPoint);
- }
- },
-
- /**
- * !#en
- * Converts a local Point to world space coordinates.The result is in Points.
- * treating the returned/received node point as anchor relative.
- * !#zh
- * 将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
- * 返回值将基于世界坐标。
- * @method convertToWorldSpaceAR
- * @param {Vec2} nodePoint
- * @return {Vec2}
- * @example
- * var newVec2 = node.convertToWorldSpaceAR(cc.v2(100, 100));
- */
- convertToWorldSpaceAR: function (nodePoint) {
- if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
- // ensure transform computed
- cc.director._visitScene();
- }
- if (this._sgNode.isIgnoreAnchorPointForPosition()) {
- // see https://github.com/cocos-creator/engine/pull/391
- return cc.v2(this._sgNode.convertToWorldSpace(nodePoint));
- }
- else {
- return cc.v2(this._sgNode.convertToWorldSpaceAR(nodePoint));
- }
- },
-
- /**
- * !#en convenience methods which take a cc.Touch instead of cc.Vec2.
- * !#zh 将触摸点转换成本地坐标系中位置。
- * @method convertTouchToNodeSpace
- * @param {Touch} touch - The touch object
- * @return {Vec2}
- * @example
- * var newVec2 = node.convertTouchToNodeSpace(touch);
- */
- convertTouchToNodeSpace: function (touch) {
- return this.convertToNodeSpace(touch.getLocation());
- },
-
- /**
- * !#en converts a cc.Touch (world coordinates) into a local coordinate. This method is AR (Anchor Relative).
- * !#zh 转换一个 cc.Touch(世界坐标)到一个局部坐标,该方法基于节点坐标。
- * @method convertTouchToNodeSpaceAR
- * @param {Touch} touch - The touch object
- * @return {Vec2}
- * @example
- * var newVec2 = node.convertTouchToNodeSpaceAR(touch);
- */
- convertTouchToNodeSpaceAR: function (touch) {
- return this.convertToNodeSpaceAR(touch.getLocation());
- },
-
/**
* !#en
* Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
From 5f966434a96710605000084dcaf528750940c266 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 11:12:05 +0800
Subject: [PATCH 17/36] move getNodeToXXXTransform/getXXXToNodeTransform
functions to CCNode
---
cocos2d/core/CCNode.js | 128 ++++++++++++++++++++++++++++++
cocos2d/core/utils/base-node.js | 133 +-------------------------------
2 files changed, 132 insertions(+), 129 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 5a61dfb18e..63f0892b48 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2460,6 +2460,134 @@ var Node = cc.Class({
return rect;
},
+ /**
+ * !#en
+ * Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
+ * The matrix is in Pixels.
+ * !#zh 返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。这个矩阵以像素为单位。
+ * @method getNodeToParentTransform
+ * @return {AffineTransform} The affine transform object
+ * @example
+ * var affineTransform = node.getNodeToParentTransform();
+ */
+ getNodeToParentTransform: function () {
+ var contentSize = this.getContentSize();
+ var mat = this._sgNode.getNodeToParentTransform();
+ if (this._isSgTransformArToMe(contentSize)) {
+ // see getNodeToWorldTransform
+ var tx = - this._anchorPoint.x * contentSize.width;
+ var ty = - this._anchorPoint.y * contentSize.height;
+ var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
+ mat = cc.affineTransformConcatIn(offset, mat);
+ }
+ return mat;
+ },
+
+ /**
+ * !#en Returns the world affine transform matrix. The matrix is in Pixels.
+ * !#zh 返回节点到世界坐标系的仿射变换矩阵。矩阵单位是像素。
+ * @method getNodeToWorldTransform
+ * @return {AffineTransform}
+ * @example
+ * var affineTransform = node.getNodeToWorldTransform();
+ */
+ getNodeToWorldTransform: function () {
+ var contentSize = this.getContentSize();
+
+ if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ // ensure transform computed
+ cc.director._visitScene();
+ }
+ var mat = this._sgNode.getNodeToWorldTransform();
+
+ if (this._isSgTransformArToMe(contentSize)) {
+ // _sgNode.getNodeToWorldTransform is not anchor relative (AR), in this case,
+ // we should translate to bottem left to consistent with it
+ // see https://github.com/cocos-creator/engine/pull/391
+ var tx = - this._anchorPoint.x * contentSize.width;
+ var ty = - this._anchorPoint.y * contentSize.height;
+ var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
+ mat = cc.affineTransformConcatIn(offset, mat);
+ }
+ return mat;
+ },
+
+ /**
+ * !#en
+ * Returns the world affine transform matrix. The matrix is in Pixels.
+ * This method is AR (Anchor Relative).
+ * !#zh
+ * 返回节点到世界坐标仿射变换矩阵。矩阵单位是像素。
+ * 该方法基于节点坐标。
+ * @method getNodeToWorldTransformAR
+ * @return {AffineTransform}
+ * @example
+ * var mat = node.getNodeToWorldTransformAR();
+ */
+ getNodeToWorldTransformAR: function () {
+ var contentSize = this.getContentSize();
+
+ if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ // ensure transform computed
+ cc.director._visitScene();
+ }
+ var mat = this._sgNode.getNodeToWorldTransform();
+
+ if ( !this._isSgTransformArToMe(contentSize) ) {
+ // see getNodeToWorldTransform
+ var tx = this._anchorPoint.x * contentSize.width;
+ var ty = this._anchorPoint.y * contentSize.height;
+ var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
+ mat = cc.affineTransformConcatIn(offset, mat);
+ }
+ return mat;
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 返回将父节点的坐标系转换成节点(局部)的空间坐标系的矩阵。
+ * 该矩阵以像素为单位。返回的矩阵是只读的,不能更改。
+ * @method getParentToNodeTransform
+ * @return {AffineTransform}
+ * @example
+ * var affineTransform = node.getParentToNodeTransform();
+ */
+ getParentToNodeTransform: function () {
+ return this._sgNode.getParentToNodeTransform();
+ },
+
+ /**
+ * !#en Returns the inverse world affine transform matrix. The matrix is in Pixels.
+ * !#en 返回世界坐标系到节点坐标系的逆矩阵。
+ * @method getWorldToNodeTransform
+ * @return {AffineTransform}
+ * @example
+ * var affineTransform = node.getWorldToNodeTransform();
+ */
+ getWorldToNodeTransform: function () {
+ if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
+ // ensure transform computed
+ cc.director._visitScene();
+ }
+ return this._sgNode.getWorldToNodeTransform();
+ },
+
+ _isSgTransformArToMe: function (myContentSize) {
+ var renderSize = this._sgNode.getContentSize();
+ if (renderSize.width === 0 && renderSize.height === 0 &&
+ (myContentSize.width !== 0 || myContentSize.height !== 0)) {
+ // anchor point ignored
+ return true;
+ }
+ if (this._sgNode.isIgnoreAnchorPointForPosition()) {
+ // sg transform become anchor relative...
+ return true;
+ }
+ return false;
+ },
/**
* !#en Converts a Point to node (local) space coordinates. The result is in Vec2.
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index d5a7c89dba..443fcce4eb 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -607,135 +607,10 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
this._sgNode.setNodeDirty();
},
- /**
- * !#en
- * 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.
- * !#zh
- * 返回将父节点的坐标系转换成节点(局部)的空间坐标系的矩阵。
- * 该矩阵以像素为单位。返回的矩阵是只读的,不能更改。
- * @method getParentToNodeTransform
- * @return {AffineTransform}
- * @example
- * var affineTransform = node.getParentToNodeTransform();
- */
- getParentToNodeTransform: function () {
- return this._sgNode.getParentToNodeTransform();
- },
-
- _isSgTransformArToMe: function (myContentSize) {
- var renderSize = this._sgNode.getContentSize();
- if (renderSize.width === 0 && renderSize.height === 0 &&
- (myContentSize.width !== 0 || myContentSize.height !== 0)) {
- // anchor point ignored
- return true;
- }
- if (this._sgNode.isIgnoreAnchorPointForPosition()) {
- // sg transform become anchor relative...
- return true;
- }
- return false;
- },
-
- /**
- * !#en Returns the world affine transform matrix. The matrix is in Pixels.
- * !#zh 返回节点到世界坐标系的仿射变换矩阵。矩阵单位是像素。
- * @method getNodeToWorldTransform
- * @return {AffineTransform}
- * @example
- * var affineTransform = node.getNodeToWorldTransform();
- */
- getNodeToWorldTransform: function () {
- var contentSize = this.getContentSize();
-
- if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
- // ensure transform computed
- cc.director._visitScene();
- }
- var mat = this._sgNode.getNodeToWorldTransform();
-
- if (this._isSgTransformArToMe(contentSize)) {
- // _sgNode.getNodeToWorldTransform is not anchor relative (AR), in this case,
- // we should translate to bottem left to consistent with it
- // see https://github.com/cocos-creator/engine/pull/391
- var tx = - this._anchorPoint.x * contentSize.width;
- var ty = - this._anchorPoint.y * contentSize.height;
- var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
- mat = cc.affineTransformConcatIn(offset, mat);
- }
- return mat;
- },
-
-
- /**
- * !#en
- * Returns the world affine transform matrix. The matrix is in Pixels.
- * This method is AR (Anchor Relative).
- * !#zh
- * 返回节点到世界坐标仿射变换矩阵。矩阵单位是像素。
- * 该方法基于节点坐标。
- * @method getNodeToWorldTransformAR
- * @return {AffineTransform}
- * @example
- * var mat = node.getNodeToWorldTransformAR();
- */
- getNodeToWorldTransformAR: function () {
- var contentSize = this.getContentSize();
-
- if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
- // ensure transform computed
- cc.director._visitScene();
- }
- var mat = this._sgNode.getNodeToWorldTransform();
-
- if ( !this._isSgTransformArToMe(contentSize) ) {
- // see getNodeToWorldTransform
- var tx = this._anchorPoint.x * contentSize.width;
- var ty = this._anchorPoint.y * contentSize.height;
- var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
- mat = cc.affineTransformConcatIn(offset, mat);
- }
- return mat;
- },
- /**
- * !#en Returns the inverse world affine transform matrix. The matrix is in Pixels.
- * !#en 返回世界坐标系到节点坐标系的逆矩阵。
- * @method getWorldToNodeTransform
- * @return {AffineTransform}
- * @example
- * var affineTransform = node.getWorldToNodeTransform();
- */
- getWorldToNodeTransform: function () {
- if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
- // ensure transform computed
- cc.director._visitScene();
- }
- return this._sgNode.getWorldToNodeTransform();
- },
-
- /**
- * !#en
- * Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
- * The matrix is in Pixels.
- * !#zh 返回这个将节点(局部)的空间坐标系转换成父节点的空间坐标系的矩阵。这个矩阵以像素为单位。
- * @method getNodeToParentTransform
- * @return {AffineTransform} The affine transform object
- * @example
- * var affineTransform = node.getNodeToParentTransform();
- */
- getNodeToParentTransform: function () {
- var contentSize = this.getContentSize();
- var mat = this._sgNode.getNodeToParentTransform();
- if (this._isSgTransformArToMe(contentSize)) {
- // see getNodeToWorldTransform
- var tx = - this._anchorPoint.x * contentSize.width;
- var ty = - this._anchorPoint.y * contentSize.height;
- var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
- mat = cc.affineTransformConcatIn(offset, mat);
- }
- return mat;
- },
-
+ getNodeToParentTransform: null,
+ getNodeToWorldTransform: null,
+ getParentToNodeTransform: null,
+ getWorldToNodeTransform: null,
// HIERARCHY METHODS
From dade7424ad6bee7507934db59f82c926d8283752 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 12:07:31 +0800
Subject: [PATCH 18/36] base node is clean now
---
cocos2d/core/CCNode.js | 502 ++++++++++++++++++++++++++++++-
cocos2d/core/utils/base-node.js | 507 +-------------------------------
2 files changed, 511 insertions(+), 498 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 63f0892b48..b9d917c04f 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -25,6 +25,7 @@
'use strict';
var PrefabHelper = require('./utils/prefab-helper');
+var SgHelper = require('./utils/scene-graph-helper');
var JS = cc.js;
var Flags = cc.Object.Flags;
@@ -339,6 +340,23 @@ var Node = cc.Class({
extends: require('./utils/base-node'),
properties: {
+ // SERIALIZABLE
+ _opacity: 255,
+ _color: cc.Color.WHITE,
+ _cascadeOpacityEnabled: true,
+ _anchorPoint: cc.p(0.5, 0.5),
+ _contentSize: cc.size(0, 0),
+ _rotationX: 0,
+ _rotationY: 0.0,
+ _scaleX: 1.0,
+ _scaleY: 1.0,
+ _position: cc.p(0, 0),
+ _skewX: 0,
+ _skewY: 0,
+ _localZOrder: 0,
+ _globalZOrder: 0,
+ _opacityModifyRGB: false,
+
/**
* !#en
* The local active state of this node.
@@ -985,6 +1003,43 @@ var Node = cc.Class({
if (CC_JSB) {
this._retainedActions = [];
}
+
+ /**
+ * Current scene graph node for this node.
+ *
+ * @property _sgNode
+ * @type {_ccsg.Node}
+ * @private
+ */
+ var sgNode = this._sgNode = new _ccsg.Node();
+ if (CC_JSB) {
+ sgNode.retain();
+ sgNode._entity = this;
+ sgNode.onEnter = function () {
+ _ccsg.Node.prototype.onEnter.call(this);
+ if (this._entity && !this._entity._active) {
+ cc.director.getActionManager().pauseTarget(this);
+ cc.eventManager.pauseTarget(this);
+ }
+ };
+ }
+ if (!cc.game._isCloning) {
+ sgNode.cascadeOpacity = true;
+ }
+
+ /**
+ * Current active size provider for this node.
+ * Size provider can equals to this._sgNode.
+ *
+ * @property _sizeProvider
+ * @type {_ccsg.Node}
+ * @private
+ */
+ this._sizeProvider = null;
+
+ this.__ignoreAnchor = false;
+ this._reorderChildDirty = false;
+
},
//statics: {
@@ -2038,6 +2093,61 @@ var Node = cc.Class({
}
},
+ setSiblingIndex: function (index) {
+ if (!this._parent) {
+ return;
+ }
+ var array = this._parent._children;
+ index = index !== -1 ? index : array.length - 1;
+ var oldIndex = array.indexOf(this);
+ if (index !== oldIndex) {
+ array.splice(oldIndex, 1);
+ if (index < array.length) {
+ array.splice(index, 0, this);
+ }
+ else {
+ array.push(this);
+ }
+
+ // update rendering scene graph, sort them by arrivalOrder
+ var parent = this._parent;
+ var siblings = parent._children;
+
+ var i = 0, len = siblings.length, sibling;
+ if (CC_JSB) {
+ if (cc.runtime) {
+ for (; i < len; i++) {
+ sibling = siblings[i]._sgNode;
+ // Reset zorder to update their arrival order
+ var zOrder = sibling.getLocalZOrder();
+ sibling.setLocalZOrder(zOrder + 1);
+ sibling.setLocalZOrder(zOrder);
+ }
+ }
+ else {
+ parent._sgNode.removeChild(this._sgNode, false);
+ if (index + 1 < array.length) {
+ var nextSibling = array[index + 1];
+ parent._sgNode.insertChildBefore(this._sgNode, nextSibling._sgNode);
+ }
+ else {
+ parent._sgNode.addChild(this._sgNode);
+ }
+ }
+ }
+ else {
+ for (; i < len; i++) {
+ sibling = siblings[i]._sgNode;
+ sibling._arrivalOrder = i;
+ cc.eventManager._setDirtyForNode(sibling);
+ }
+ cc.renderer.childrenOrderDirty = true;
+ parent._sgNode._reorderChildDirty = true;
+ parent._delaySort();
+ }
+ }
+ },
+
//end override
/**
@@ -2160,6 +2270,73 @@ var Node = cc.Class({
}
},
+ /**
+ * !#en
+ * 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.
+ * !#zh 获取节点自身大小,不受该节点是否被缩放或者旋转的影响。
+ * @method getContentSize
+ * @param {Boolean} [ignoreSizeProvider=false] - true if you need to get the original size of the node
+ * @return {Size} The untransformed size of the node.
+ * @example
+ * cc.log("Content Size: " + node.getContentSize());
+ */
+ getContentSize: function (ignoreSizeProvider) {
+ if (this._sizeProvider && !ignoreSizeProvider) {
+ var size = this._sizeProvider.getContentSize();
+ this._contentSize = size;
+ return size;
+ }
+ else {
+ return cc.size(this._contentSize);
+ }
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh 设置节点原始大小,不受该节点是否被缩放或者旋转的影响。
+ * @method setContentSize
+ * @param {Size|Number} size - The untransformed size of the node or The untransformed size's width of the node.
+ * @param {Number} [height] - The untransformed size's height of the node.
+ * @example
+ * node.setContentSize(cc.size(100, 100));
+ * node.setContentSize(100, 100);
+ */
+ setContentSize: function (size, height) {
+ var locContentSize = this._contentSize;
+ var clone;
+ if (height === undefined) {
+ if ((size.width === locContentSize.width) && (size.height === locContentSize.height))
+ return;
+ if (CC_EDITOR) {
+ clone = cc.size(locContentSize);
+ }
+ locContentSize.width = size.width;
+ locContentSize.height = size.height;
+ } else {
+ if ((size === locContentSize.width) && (height === locContentSize.height))
+ return;
+ if (CC_EDITOR) {
+ clone = cc.size(locContentSize);
+ }
+ locContentSize.width = size;
+ locContentSize.height = height;
+ }
+ if (this._sizeProvider) {
+ this._sizeProvider.setContentSize(locContentSize);
+ }
+ if (CC_EDITOR) {
+ this.emit(SIZE_CHANGED, clone);
+ }
+ else {
+ this.emit(SIZE_CHANGED);
+ }
+ },
+
/**
* !#en
* Set whether color should be changed with the opacity value,
@@ -2705,7 +2882,330 @@ var Node = cc.Class({
convertTouchToNodeSpaceAR: function (touch) {
return this.convertToNodeSpaceAR(touch.getLocation());
},
-
+
+ setNodeDirty: function(){
+ this._sgNode.setNodeDirty();
+ },
+
+ /**
+ * !#en
+ * "add" logic MUST only be in this method
+ * !#zh
+ * 添加子节点,并且可以修改该节点的 局部 Z 顺序和标签。
+ * @method addChild
+ * @param {Node} child - A child node
+ * @param {Number} [localZOrder] - Z order for drawing priority. Please refer to setZOrder(int)
+ * @param {Number|String} [tag] - An integer or a name to identify the node easily. Please refer to setTag(int) and setName(string)
+ * @example
+ * node.addChild(newNode, 1, 1001);
+ */
+ addChild: function (child, localZOrder, tag) {
+ localZOrder = localZOrder === undefined ? child._localZOrder : localZOrder;
+ var name, setTag = false;
+ if(typeof tag === 'undefined'){
+ tag = undefined;
+ name = child._name;
+ } else if(cc.js.isString(tag)){
+ name = tag;
+ tag = undefined;
+ } else if(cc.js.isNumber(tag)){
+ setTag = true;
+ name = "";
+ }
+
+ if (CC_DEV && !(child instanceof cc.Node)) {
+ return cc.errorID(1634, cc.js.getClassName(child));
+ }
+ cc.assertID(child, 1606);
+ cc.assertID(child._parent === null, 1605);
+
+ // invokes the parent setter
+ child.parent = this;
+
+ child.zIndex = localZOrder;
+ if (setTag)
+ child.setTag(tag);
+ else
+ child.setName(name);
+ },
+
+ /**
+ * !#en Stops all running actions and schedulers.
+ * !#zh 停止所有正在播放的动作和计时器。
+ * @method cleanup
+ * @example
+ * node.cleanup();
+ */
+ cleanup: function () {
+ // actions
+ cc.director.getActionManager().removeAllActionsFromTarget(this);
+ // event
+ cc.eventManager.removeListeners(this);
+
+ // children
+ var i, len = this._children.length, node;
+ for (i = 0; i < len; ++i) {
+ node = this._children[i];
+ if (node)
+ node.cleanup();
+ }
+ },
+
+ // composition: REMOVE
+
+ /**
+ * !#en
+ * Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks.
+ * If the cleanup parameter is not passed, it will force a cleanup.
+ * If the node orphan, then nothing happens.
+ * !#zh
+ * 从父节点中删除一个节点。cleanup 参数为 true,那么在这个节点上所有的动作和回调都会被删除,反之则不会。
+ * 如果不传入 cleanup 参数,默认是 true 的。
+ * 如果这个节点是一个孤节点,那么什么都不会发生。
+ * @method removeFromParent
+ * @param {Boolean} [cleanup=true] - true if all actions and callbacks on this node should be removed, false otherwise.
+ * @see cc.Node#removeFromParentAndCleanup
+ * @example
+ * node.removeFromParent();
+ * node.removeFromParent(false);
+ */
+ removeFromParent: function (cleanup) {
+ if (this._parent) {
+ if (cleanup === undefined)
+ cleanup = true;
+ this._parent.removeChild(this, cleanup);
+ }
+ },
+
+ /**
+ * !#en
+ * Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
+ * If the cleanup parameter is not passed, it will force a cleanup.
+ * "remove" logic MUST only be on this method
+ * If a class wants to extend the 'removeChild' behavior it only needs
+ * to override this method.
+ * !#zh
+ * 移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
+ * 如果 cleanup 参数不传入,默认为 true 表示清理。
+ * @method removeChild
+ * @param {Node} child - The child node which will be removed.
+ * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
+ * @example
+ * node.removeChild(newNode);
+ * node.removeChild(newNode, false);
+ */
+ removeChild: function (child, cleanup) {
+ if (this._children.indexOf(child) > -1) {
+ // If you don't do cleanup, the child's actions will not get removed and the
+ if (cleanup || cleanup === undefined) {
+ child.cleanup();
+ }
+ // invoke the parent setter
+ child.parent = null;
+ }
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 通过标签移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
+ * 如果 cleanup 参数不传入,默认为 true 表示清理。
+ * @method removeChildByTag
+ * @param {Number} tag - An integer number that identifies a child node
+ * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
+ * @see cc.Node#removeChildByTag
+ * @example
+ * node.removeChildByTag(1001);
+ * node.removeChildByTag(1001, false);
+ */
+ removeChildByTag: function (tag, cleanup) {
+ if (tag === cc.macro.NODE_TAG_INVALID)
+ cc.logID(1609);
+
+ var child = this.getChildByTag(tag);
+ if (!child)
+ cc.logID(1610, tag);
+ else
+ this.removeChild(child, cleanup);
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 移除节点所有的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
+ * 如果 cleanup 参数不传入,默认为 true 表示清理。
+ * @method removeAllChildren
+ * @param {Boolean} [cleanup=true] - true if all running actions on all children nodes should be cleanup, false otherwise.
+ * @example
+ * node.removeAllChildren();
+ * node.removeAllChildren(false);
+ */
+ removeAllChildren: function (cleanup) {
+ // not using detachChild improves speed here
+ var children = this._children;
+ if (cleanup === undefined)
+ cleanup = true;
+ for (var i = children.length - 1; i >= 0; i--) {
+ var node = children[i];
+ if (node) {
+ //if (this._running) {
+ // node.onExitTransitionDidStart();
+ // node.onExit();
+ //}
+
+ // If you don't do cleanup, the node's actions will not get removed and the
+ if (cleanup)
+ node.cleanup();
+
+ node.parent = null;
+ }
+ }
+ this._children.length = 0;
+ },
+
+ /**
+ * !#en Sorts the children array depends on children's zIndex and arrivalOrder,
+ * normally you won't need to invoke this function.
+ * !#zh 根据子节点的 zIndex 和 arrivalOrder 进行排序,正常情况下开发者不需要手动调用这个函数。
+ *
+ * @method sortAllChildren
+ */
+ sortAllChildren: function () {
+ if (this._reorderChildDirty) {
+ this._reorderChildDirty = false;
+ var _children = this._children;
+ if (_children.length > 1) {
+ // insertion sort
+ var len = _children.length, i, j, child;
+ for (i = 1; i < len; i++){
+ child = _children[i];
+ j = i - 1;
+
+ //continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
+ while(j >= 0){
+ if (child._localZOrder < _children[j]._localZOrder) {
+ _children[j+1] = _children[j];
+ } else if (child._localZOrder === _children[j]._localZOrder &&
+ child._sgNode._arrivalOrder < _children[j]._sgNode._arrivalOrder) {
+ _children[j+1] = _children[j];
+ } else {
+ break;
+ }
+ j--;
+ }
+ _children[j+1] = child;
+ }
+ this.emit(CHILD_REORDER);
+ }
+ cc.director.__fastOff(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this, this.__eventTargets);
+ }
+ },
+
+ _delaySort: function () {
+ if (!this._reorderChildDirty) {
+ this._reorderChildDirty = true;
+ cc.director.__fastOn(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this, this.__eventTargets);
+ }
+ },
+
+ _updateDummySgNode: function () {
+ var self = this;
+ var sgNode = self._sgNode;
+
+ sgNode.setPosition(self._position);
+ sgNode.setRotationX(self._rotationX);
+ sgNode.setRotationY(self._rotationY);
+ sgNode.setScale(self._scaleX, self._scaleY);
+ sgNode.setSkewX(self._skewX);
+ sgNode.setSkewY(self._skewY);
+ sgNode.setIgnoreAnchorPointForPosition(self.__ignoreAnchor);
+
+ var arrivalOrder = sgNode._arrivalOrder;
+ sgNode.setLocalZOrder(self._localZOrder);
+ sgNode._arrivalOrder = arrivalOrder; // revert arrivalOrder changed in setLocalZOrder
+
+ sgNode.setGlobalZOrder(self._globalZOrder);
+
+ if (CC_JSB) {
+ // fix tintTo and tintBy action for jsb displays err for fireball/issues/4137
+ sgNode.setColor(this._color);
+ }
+ sgNode.setOpacity(self._opacity);
+ sgNode.setOpacityModifyRGB(self._opacityModifyRGB);
+ sgNode.setCascadeOpacityEnabled(self._cascadeOpacityEnabled);
+ sgNode.setTag(self._tag);
+ },
+
+ _updateSgNode: function () {
+ this._updateDummySgNode();
+ var sgNode = this._sgNode;
+ sgNode.setAnchorPoint(this._anchorPoint);
+ sgNode.setVisible(this._active);
+ sgNode.setColor(this._color);
+
+ // update ActionManager and EventManager because sgNode maybe changed
+ if (this._activeInHierarchy) {
+ cc.director.getActionManager().resumeTarget(this);
+ cc.eventManager.resumeTarget(this);
+ }
+ else {
+ cc.director.getActionManager().pauseTarget(this);
+ cc.eventManager.pauseTarget(this);
+ }
+ },
+
+ _removeSgNode: SgHelper.removeSgNode,
+
+ onRestore: CC_EDITOR && function () {
+ this._updateDummySgNode();
+
+ var sizeProvider = this._sizeProvider;
+ if (sizeProvider) {
+ sizeProvider.setContentSize(this._contentSize);
+ if (sizeProvider instanceof _ccsg.Node) {
+ sizeProvider.setAnchorPoint(this._anchorPoint);
+ sizeProvider.setColor(this._color);
+ if (sizeProvider !== this._sgNode) {
+ sizeProvider.ignoreAnchor = this.__ignoreAnchor;
+ sizeProvider.setOpacityModifyRGB(this._opacityModifyRGB);
+ if ( !this._cascadeOpacityEnabled ) {
+ sizeProvider.setOpacity(this._opacity);
+ }
+ }
+ }
+ }
+
+ var sgParent = this._parent && this._parent._sgNode;
+ if (this._sgNode._parent !== sgParent) {
+ if (this._sgNode._parent) {
+ this._sgNode.removeFromParent();
+ }
+ if (sgParent) {
+ sgParent.addChild(this._sgNode);
+ }
+ }
+
+ // check activity state
+ var shouldActiveInHierarchy = (this._parent && this._parent._activeInHierarchy && this._active);
+ if (shouldActiveInHierarchy !== this._activeInHierarchy) {
+ this._onActivatedInHierarchy(shouldActiveInHierarchy);
+ this.emit('active-in-hierarchy-changed', this);
+ }
+
+ if (this._activeInHierarchy) {
+ cc.director.getActionManager().resumeTarget(this);
+ cc.eventManager.resumeTarget(this);
+ }
+ else {
+ cc.director.getActionManager().pauseTarget(this);
+ cc.eventManager.pauseTarget(this);
+ }
+ },
+
//functions moved from base node end
});
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 443fcce4eb..071939821a 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -62,24 +62,10 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
// SERIALIZABLE
- _opacity: 255,
- _color: cc.Color.WHITE,
- _cascadeOpacityEnabled: true,
_parent: null,
- _anchorPoint: cc.p(0.5, 0.5),
- _contentSize: cc.size(0, 0),
_children: [],
- _rotationX: 0,
- _rotationY: 0.0,
- _scaleX: 1.0,
- _scaleY: 1.0,
- _position: cc.p(0, 0),
- _skewX: 0,
- _skewY: 0,
- _localZOrder: 0,
- _globalZOrder: 0,
+
_tag: cc.macro.NODE_TAG_INVALID,
- _opacityModifyRGB: false,
// API
@@ -186,42 +172,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
ctor: function () {
- /**
- * Current scene graph node for this node.
- *
- * @property _sgNode
- * @type {_ccsg.Node}
- * @private
- */
- var sgNode = this._sgNode = new _ccsg.Node();
- if (CC_JSB) {
- sgNode.retain();
- sgNode._entity = this;
- sgNode.onEnter = function () {
- _ccsg.Node.prototype.onEnter.call(this);
- if (this._entity && !this._entity._active) {
- cc.director.getActionManager().pauseTarget(this);
- cc.eventManager.pauseTarget(this);
- }
- };
- }
- if (!cc.game._isCloning) {
- sgNode.cascadeOpacity = true;
- }
-
- /**
- * Current active size provider for this node.
- * Size provider can equals to this._sgNode.
- *
- * @property _sizeProvider
- * @type {_ccsg.Node}
- * @private
- */
- this._sizeProvider = null;
-
- this.__ignoreAnchor = false;
- this._reorderChildDirty = false;
-
// Support for ActionManager and EventManager
this.__instanceId = this._id || cc.ClassManager.getNewInstanceId();
@@ -239,13 +189,15 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return this._tag;
},
+ setTag: function (tag) {
+ this._tag = tag;
+ },
getParent: function() {
return this._parent;
},
//interface need to be implemented in derived classes
- setTag: null,
setParent: null,
@@ -284,96 +236,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- /**
- * !#en
- * 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.
- * !#zh 获取节点自身大小,不受该节点是否被缩放或者旋转的影响。
- * @method getContentSize
- * @param {Boolean} [ignoreSizeProvider=false] - true if you need to get the original size of the node
- * @return {Size} The untransformed size of the node.
- * @example
- * cc.log("Content Size: " + node.getContentSize());
- */
- getContentSize: function (ignoreSizeProvider) {
- if (this._sizeProvider && !ignoreSizeProvider) {
- var size = this._sizeProvider.getContentSize();
- this._contentSize = size;
- return size;
- }
- else {
- return cc.size(this._contentSize);
- }
- },
-
- /**
- * !#en
- * 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.
- * !#zh 设置节点原始大小,不受该节点是否被缩放或者旋转的影响。
- * @method setContentSize
- * @param {Size|Number} size - The untransformed size of the node or The untransformed size's width of the node.
- * @param {Number} [height] - The untransformed size's height of the node.
- * @example
- * node.setContentSize(cc.size(100, 100));
- * node.setContentSize(100, 100);
- */
- setContentSize: function (size, height) {
- var locContentSize = this._contentSize;
- var clone;
- if (height === undefined) {
- if ((size.width === locContentSize.width) && (size.height === locContentSize.height))
- return;
- if (CC_EDITOR) {
- clone = cc.size(locContentSize);
- }
- locContentSize.width = size.width;
- locContentSize.height = size.height;
- } else {
- if ((size === locContentSize.width) && (height === locContentSize.height))
- return;
- if (CC_EDITOR) {
- clone = cc.size(locContentSize);
- }
- locContentSize.width = size;
- locContentSize.height = height;
- }
- if (this._sizeProvider) {
- this._sizeProvider.setContentSize(locContentSize);
- }
- if (CC_EDITOR) {
- this.emit(SIZE_CHANGED, clone);
- }
- else {
- this.emit(SIZE_CHANGED);
- }
- },
-
-
- /**
- * !#en Stops all running actions and schedulers.
- * !#zh 停止所有正在播放的动作和计时器。
- * @method cleanup
- * @example
- * node.cleanup();
- */
- cleanup: function () {
- // actions
- cc.director.getActionManager().removeAllActionsFromTarget(this);
- // event
- cc.eventManager.removeListeners(this);
-
- // children
- var i, len = this._children.length, node;
- for (i = 0; i < len; ++i) {
- node = this._children[i];
- if (node)
- node.cleanup();
- }
- },
-
// composition: GET
/**
@@ -444,168 +306,11 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
},
// composition: ADD
-
- /**
- * !#en
- * "add" logic MUST only be in this method
- * !#zh
- * 添加子节点,并且可以修改该节点的 局部 Z 顺序和标签。
- * @method addChild
- * @param {Node} child - A child node
- * @param {Number} [localZOrder] - Z order for drawing priority. Please refer to setZOrder(int)
- * @param {Number|String} [tag] - An integer or a name to identify the node easily. Please refer to setTag(int) and setName(string)
- * @example
- * node.addChild(newNode, 1, 1001);
- */
- addChild: function (child, localZOrder, tag) {
- localZOrder = localZOrder === undefined ? child._localZOrder : localZOrder;
- var name, setTag = false;
- if(typeof tag === 'undefined'){
- tag = undefined;
- name = child._name;
- } else if(cc.js.isString(tag)){
- name = tag;
- tag = undefined;
- } else if(cc.js.isNumber(tag)){
- setTag = true;
- name = "";
- }
-
- if (CC_DEV && !(child instanceof cc.Node)) {
- return cc.errorID(1634, cc.js.getClassName(child));
- }
- cc.assertID(child, 1606);
- cc.assertID(child._parent === null, 1605);
-
- // invokes the parent setter
- child.parent = this;
-
- child.zIndex = localZOrder;
- if (setTag)
- child.setTag(tag);
- else
- child.setName(name);
- },
-
- // composition: REMOVE
-
- /**
- * !#en
- * Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks.
- * If the cleanup parameter is not passed, it will force a cleanup.
- * If the node orphan, then nothing happens.
- * !#zh
- * 从父节点中删除一个节点。cleanup 参数为 true,那么在这个节点上所有的动作和回调都会被删除,反之则不会。
- * 如果不传入 cleanup 参数,默认是 true 的。
- * 如果这个节点是一个孤节点,那么什么都不会发生。
- * @method removeFromParent
- * @param {Boolean} [cleanup=true] - true if all actions and callbacks on this node should be removed, false otherwise.
- * @see cc.Node#removeFromParentAndCleanup
- * @example
- * node.removeFromParent();
- * node.removeFromParent(false);
- */
- removeFromParent: function (cleanup) {
- if (this._parent) {
- if (cleanup === undefined)
- cleanup = true;
- this._parent.removeChild(this, cleanup);
- }
- },
-
- /**
- * !#en
- * Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
- * If the cleanup parameter is not passed, it will force a cleanup.
- * "remove" logic MUST only be on this method
- * If a class wants to extend the 'removeChild' behavior it only needs
- * to override this method.
- * !#zh
- * 移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
- * 如果 cleanup 参数不传入,默认为 true 表示清理。
- * @method removeChild
- * @param {Node} child - The child node which will be removed.
- * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
- * @example
- * node.removeChild(newNode);
- * node.removeChild(newNode, false);
- */
- removeChild: function (child, cleanup) {
- if (this._children.indexOf(child) > -1) {
- // If you don't do cleanup, the child's actions will not get removed and the
- if (cleanup || cleanup === undefined) {
- child.cleanup();
- }
- // invoke the parent setter
- child.parent = null;
- }
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 通过标签移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
- * 如果 cleanup 参数不传入,默认为 true 表示清理。
- * @method removeChildByTag
- * @param {Number} tag - An integer number that identifies a child node
- * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
- * @see cc.Node#removeChildByTag
- * @example
- * node.removeChildByTag(1001);
- * node.removeChildByTag(1001, false);
- */
- removeChildByTag: function (tag, cleanup) {
- if (tag === cc.macro.NODE_TAG_INVALID)
- cc.logID(1609);
-
- var child = this.getChildByTag(tag);
- if (!child)
- cc.logID(1610, tag);
- else
- this.removeChild(child, cleanup);
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 移除节点所有的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
- * 如果 cleanup 参数不传入,默认为 true 表示清理。
- * @method removeAllChildren
- * @param {Boolean} [cleanup=true] - true if all running actions on all children nodes should be cleanup, false otherwise.
- * @example
- * node.removeAllChildren();
- * node.removeAllChildren(false);
- */
- removeAllChildren: function (cleanup) {
- // not using detachChild improves speed here
- var children = this._children;
- if (cleanup === undefined)
- cleanup = true;
- for (var i = children.length - 1; i >= 0; i--) {
- var node = children[i];
- if (node) {
- //if (this._running) {
- // node.onExitTransitionDidStart();
- // node.onExit();
- //}
-
- // If you don't do cleanup, the node's actions will not get removed and the
- if (cleanup)
- node.cleanup();
-
- node.parent = null;
- }
- }
- this._children.length = 0;
- },
-
- setNodeDirty: function(){
- this._sgNode.setNodeDirty();
- },
+ addChild: null,
+ removeFromParent: null,
+ removeChild: null,
+ removeChildByTag: null,
+ removeAllChildren: null,
getNodeToParentTransform: null,
getNodeToWorldTransform: null,
@@ -639,61 +344,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* @example
* node.setSiblingIndex(1);
*/
- setSiblingIndex: function (index) {
- if (!this._parent) {
- return;
- }
- var array = this._parent._children;
- index = index !== -1 ? index : array.length - 1;
- var oldIndex = array.indexOf(this);
- if (index !== oldIndex) {
- array.splice(oldIndex, 1);
- if (index < array.length) {
- array.splice(index, 0, this);
- }
- else {
- array.push(this);
- }
-
- // update rendering scene graph, sort them by arrivalOrder
- var parent = this._parent;
- var siblings = parent._children;
-
- var i = 0, len = siblings.length, sibling;
- if (CC_JSB) {
- if (cc.runtime) {
- for (; i < len; i++) {
- sibling = siblings[i]._sgNode;
- // Reset zorder to update their arrival order
- var zOrder = sibling.getLocalZOrder();
- sibling.setLocalZOrder(zOrder + 1);
- sibling.setLocalZOrder(zOrder);
- }
- }
- else {
- parent._sgNode.removeChild(this._sgNode, false);
- if (index + 1 < array.length) {
- var nextSibling = array[index + 1];
- parent._sgNode.insertChildBefore(this._sgNode, nextSibling._sgNode);
- }
- else {
- parent._sgNode.addChild(this._sgNode);
- }
- }
- }
- else {
- for (; i < len; i++) {
- sibling = siblings[i]._sgNode;
- sibling._arrivalOrder = i;
- cc.eventManager._setDirtyForNode(sibling);
- }
- cc.renderer.childrenOrderDirty = true;
- parent._sgNode._reorderChildDirty = true;
- parent._delaySort();
- }
- }
- },
-
+ setSiblingIndex: null,
/**
* !#en Is this node a child of the given node?
* !#zh 是否是指定节点的子节点?
@@ -715,144 +366,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return false;
},
- /**
- * !#en Sorts the children array depends on children's zIndex and arrivalOrder,
- * normally you won't need to invoke this function.
- * !#zh 根据子节点的 zIndex 和 arrivalOrder 进行排序,正常情况下开发者不需要手动调用这个函数。
- *
- * @method sortAllChildren
- */
- sortAllChildren: function () {
- if (this._reorderChildDirty) {
- this._reorderChildDirty = false;
- var _children = this._children;
- if (_children.length > 1) {
- // insertion sort
- var len = _children.length, i, j, child;
- for (i = 1; i < len; i++){
- child = _children[i];
- j = i - 1;
-
- //continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
- while(j >= 0){
- if (child._localZOrder < _children[j]._localZOrder) {
- _children[j+1] = _children[j];
- } else if (child._localZOrder === _children[j]._localZOrder &&
- child._sgNode._arrivalOrder < _children[j]._sgNode._arrivalOrder) {
- _children[j+1] = _children[j];
- } else {
- break;
- }
- j--;
- }
- _children[j+1] = child;
- }
- this.emit(CHILD_REORDER);
- }
- cc.director.__fastOff(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this, this.__eventTargets);
- }
- },
-
- _delaySort: function () {
- if (!this._reorderChildDirty) {
- this._reorderChildDirty = true;
- cc.director.__fastOn(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this, this.__eventTargets);
- }
- },
-
- _updateDummySgNode: function () {
- var self = this;
- var sgNode = self._sgNode;
-
- sgNode.setPosition(self._position);
- sgNode.setRotationX(self._rotationX);
- sgNode.setRotationY(self._rotationY);
- sgNode.setScale(self._scaleX, self._scaleY);
- sgNode.setSkewX(self._skewX);
- sgNode.setSkewY(self._skewY);
- sgNode.setIgnoreAnchorPointForPosition(self.__ignoreAnchor);
-
- var arrivalOrder = sgNode._arrivalOrder;
- sgNode.setLocalZOrder(self._localZOrder);
- sgNode._arrivalOrder = arrivalOrder; // revert arrivalOrder changed in setLocalZOrder
-
- sgNode.setGlobalZOrder(self._globalZOrder);
-
- if (CC_JSB) {
- // fix tintTo and tintBy action for jsb displays err for fireball/issues/4137
- sgNode.setColor(this._color);
- }
- sgNode.setOpacity(self._opacity);
- sgNode.setOpacityModifyRGB(self._opacityModifyRGB);
- sgNode.setCascadeOpacityEnabled(self._cascadeOpacityEnabled);
- sgNode.setTag(self._tag);
- },
-
- _updateSgNode: function () {
- this._updateDummySgNode();
- var sgNode = this._sgNode;
- sgNode.setAnchorPoint(this._anchorPoint);
- sgNode.setVisible(this._active);
- sgNode.setColor(this._color);
-
- // update ActionManager and EventManager because sgNode maybe changed
- if (this._activeInHierarchy) {
- cc.director.getActionManager().resumeTarget(this);
- cc.eventManager.resumeTarget(this);
- }
- else {
- cc.director.getActionManager().pauseTarget(this);
- cc.eventManager.pauseTarget(this);
- }
- },
-
- onRestore: CC_EDITOR && function () {
- this._updateDummySgNode();
-
- var sizeProvider = this._sizeProvider;
- if (sizeProvider) {
- sizeProvider.setContentSize(this._contentSize);
- if (sizeProvider instanceof _ccsg.Node) {
- sizeProvider.setAnchorPoint(this._anchorPoint);
- sizeProvider.setColor(this._color);
- if (sizeProvider !== this._sgNode) {
- sizeProvider.ignoreAnchor = this.__ignoreAnchor;
- sizeProvider.setOpacityModifyRGB(this._opacityModifyRGB);
- if ( !this._cascadeOpacityEnabled ) {
- sizeProvider.setOpacity(this._opacity);
- }
- }
- }
- }
-
- var sgParent = this._parent && this._parent._sgNode;
- if (this._sgNode._parent !== sgParent) {
- if (this._sgNode._parent) {
- this._sgNode.removeFromParent();
- }
- if (sgParent) {
- sgParent.addChild(this._sgNode);
- }
- }
-
- // check activity state
- var shouldActiveInHierarchy = (this._parent && this._parent._activeInHierarchy && this._active);
- if (shouldActiveInHierarchy !== this._activeInHierarchy) {
- this._onActivatedInHierarchy(shouldActiveInHierarchy);
- this.emit('active-in-hierarchy-changed', this);
- }
-
- if (this._activeInHierarchy) {
- cc.director.getActionManager().resumeTarget(this);
- cc.eventManager.resumeTarget(this);
- }
- else {
- cc.director.getActionManager().pauseTarget(this);
- cc.eventManager.pauseTarget(this);
- }
- },
-
- _removeSgNode: SgHelper.removeSgNode,
});
From 663f8eae29818b143810bec3b78100c110312315 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 14:11:47 +0800
Subject: [PATCH 19/36] move common properties
_active/_components/_prefab/_persistNode to base node
---
cocos2d/core/CCNode.js | 41 -----------------------------
cocos2d/core/utils/base-node.js | 46 ++++++++++++++++++++++++++++++++-
2 files changed, 45 insertions(+), 42 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index b9d917c04f..4455278ab2 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -405,47 +405,6 @@ var Node = cc.Class({
// internal properties
- _active: true,
-
- /**
- * @property _components
- * @type {Component[]}
- * @default []
- * @readOnly
- * @private
- */
- _components: [],
-
- /**
- * The PrefabInfo object
- * @property _prefab
- * @type {PrefabInfo}
- * @private
- */
- _prefab: null,
-
- /**
- * 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.
- * @property _persistNode
- * @type {Boolean}
- * @default false
- * @private
- */
- _persistNode: {
- get: function () {
- return (this._objFlags & DontDestroy) > 0;
- },
- set: function (value) {
- if (value) {
- this._objFlags |= DontDestroy;
- }
- else {
- this._objFlags &= ~DontDestroy;
- }
- }
- },
-
/**
* !#en
* Group index of node.
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 071939821a..13d2feebb3 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -24,10 +24,13 @@
****************************************************************************/
var SgHelper = require('./scene-graph-helper');
-var Destroying = require('../platform/CCObject').Flags.Destroying;
+var Flags = require('../platform/CCObject').Flags;
var Misc = require('./misc');
var IdGenerater = require('../platform/id-generater');
+var Destroying = Flags.Destroying;
+var DontDestroy = Flags.DontDestroy;
+var Activating = Flags.Activating;
var POSITION_CHANGED = 'position-changed';
var SIZE_CHANGED = 'size-changed';
var ANCHOR_CHANGED = 'anchor-changed';
@@ -67,6 +70,47 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
_tag: cc.macro.NODE_TAG_INVALID,
+ _active: true,
+
+ /**
+ * @property _components
+ * @type {Component[]}
+ * @default []
+ * @readOnly
+ * @private
+ */
+ _components: [],
+
+ /**
+ * The PrefabInfo object
+ * @property _prefab
+ * @type {PrefabInfo}
+ * @private
+ */
+ _prefab: null,
+
+ /**
+ * 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.
+ * @property _persistNode
+ * @type {Boolean}
+ * @default false
+ * @private
+ */
+ _persistNode: {
+ get: function () {
+ return (this._objFlags & DontDestroy) > 0;
+ },
+ set: function (value) {
+ if (value) {
+ this._objFlags |= DontDestroy;
+ }
+ else {
+ this._objFlags &= ~DontDestroy;
+ }
+ }
+ },
+
// API
/**
From 21f3fad961f5b39709c4565c04befa61e0d19526 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 14:35:07 +0800
Subject: [PATCH 20/36] move some local util functions to base node
---
cocos2d/core/utils/base-node.js | 58 +++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 13d2feebb3..79942e411d 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -42,6 +42,64 @@ var ERR_INVALID_NUMBER = CC_EDITOR && 'The %s is invalid';
var idGenerater = new IdGenerater('Node');
+function getConstructor (typeOrClassName) {
+ if ( !typeOrClassName ) {
+ cc.errorID(3804);
+ return null;
+ }
+ if (typeof typeOrClassName === 'string') {
+ return JS.getClassByName(typeOrClassName);
+ }
+
+ return typeOrClassName;
+}
+
+function findComponent (node, constructor) {
+ for (var i = 0; i < node._components.length; ++i) {
+ var comp = node._components[i];
+ if (comp instanceof constructor) {
+ return comp;
+ }
+ }
+ return null;
+}
+
+function findComponents (node, constructor, components) {
+ for (var i = 0; i < node._components.length; ++i) {
+ var comp = node._components[i];
+ if (comp instanceof constructor) {
+ components.push(comp);
+ }
+ }
+}
+
+function findChildComponent (children, constructor) {
+ for (var i = 0; i < children.length; ++i) {
+ var node = children[i];
+ var comp = findComponent(node, constructor);
+ if (comp) {
+ return comp;
+ }
+ else if (node.children.length > 0) {
+ comp = findChildComponent(node.children, constructor);
+ if (comp) {
+ return comp;
+ }
+ }
+ }
+ return null;
+}
+
+function findChildComponents (children, constructor, components) {
+ for (var i = 0; i < children.length; ++i) {
+ var node = children[i];
+ findComponents(node, constructor, components);
+ if (node._children.length > 0) {
+ findChildComponents(node._children, constructor, components);
+ }
+ }
+}
+
/**
* A base node for CCNode and CCEScene, it will:
* - provide the same api with origin cocos2d rendering node (SGNode)
From 3b3f61020a6c06a4019ce6dec93f73cecdd439ba Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 16:05:27 +0800
Subject: [PATCH 21/36] move some components functions to base node
---
cocos2d/core/CCNode.js | 302 -------------------------------
cocos2d/core/utils/base-node.js | 305 +++++++++++++++++++++++++++++++-
2 files changed, 304 insertions(+), 303 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 4455278ab2..4b214afac4 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -946,8 +946,6 @@ var Node = cc.Class({
},
ctor: function (name) {
- this._name = typeof name !== 'undefined' ? name : 'New Node';
- this._activeInHierarchy = false;
// cache component
this._widget = null;
@@ -1102,306 +1100,6 @@ var Node = cc.Class({
}
},
- // COMPONENT
-
- /**
- * !#en
- * 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.
- * !#zh
- * 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
- * 传入参数也可以是脚本的名称。
- * @method getComponent
- * @param {Function|String} typeOrClassName
- * @return {Component}
- * @example
- * // get sprite component.
- * var sprite = node.getComponent(cc.Sprite);
- * // get custom test calss.
- * var test = node.getComponent("Test");
- */
- getComponent: function (typeOrClassName) {
- var constructor = getConstructor(typeOrClassName);
- if (constructor) {
- return findComponent(this, constructor);
- }
- return null;
- },
-
- /**
- * !#en Returns all components of supplied type in the node.
- * !#zh 返回节点上指定类型的所有组件。
- * @method getComponents
- * @param {Function|String} typeOrClassName
- * @return {Component[]}
- * @example
- * var sprites = node.getComponents(cc.Sprite);
- * var tests = node.getComponents("Test");
- */
- getComponents: function (typeOrClassName) {
- var constructor = getConstructor(typeOrClassName), components = [];
- if (constructor) {
- findComponents(this, constructor, components);
- }
- return components;
- },
-
- /**
- * !#en Returns the component of supplied type in any of its children using depth first search.
- * !#zh 递归查找所有子节点中第一个匹配指定类型的组件。
- * @method getComponentInChildren
- * @param {Function|String} typeOrClassName
- * @return {Component}
- * @example
- * var sprite = node.getComponentInChildren(cc.Sprite);
- * var Test = node.getComponentInChildren("Test");
- */
- getComponentInChildren: function (typeOrClassName) {
- var constructor = getConstructor(typeOrClassName);
- if (constructor) {
- return findChildComponent(this._children, constructor);
- }
- return null;
- },
-
- /**
- * !#en Returns all components of supplied type in self or any of its children.
- * !#zh 递归查找自身或所有子节点中指定类型的组件
- * @method getComponentsInChildren
- * @param {Function|String} typeOrClassName
- * @return {Component[]}
- * @example
- * var sprites = node.getComponentsInChildren(cc.Sprite);
- * var tests = node.getComponentsInChildren("Test");
- */
- getComponentsInChildren: function (typeOrClassName) {
- var constructor = getConstructor(typeOrClassName), components = [];
- if (constructor) {
- findComponents(this, constructor, components);
- findChildComponents(this._children, constructor, components);
- }
- return components;
- },
-
- _checkMultipleComp: CC_EDITOR && function (ctor) {
- var existing = this.getComponent(ctor._disallowMultiple);
- if (existing) {
- if (existing.constructor === ctor) {
- cc.errorID(3805, JS.getClassName(ctor), this._name);
- }
- else {
- cc.errorID(3806, JS.getClassName(ctor), this._name, JS.getClassName(existing));
- }
- return false;
- }
- return true;
- },
-
- /**
- * !#en Adds a component class to the node. You can also add component to node by passing in the name of the script.
- * !#zh 向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。
- * @method addComponent
- * @param {Function|String} typeOrClassName - The constructor or the class name of the component to add
- * @return {Component} - The newly added component
- * @example
- * var sprite = node.addComponent(cc.Sprite);
- * var test = node.addComponent("Test");
- */
- addComponent: function (typeOrClassName) {
-
- if (CC_EDITOR && (this._objFlags & Destroying)) {
- cc.error('isDestroying');
- return null;
- }
-
- // get component
-
- var constructor;
- if (typeof typeOrClassName === 'string') {
- constructor = JS.getClassByName(typeOrClassName);
- if ( !constructor ) {
- cc.errorID(3807, typeOrClassName);
- if (cc._RFpeek()) {
- cc.errorID(3808, typeOrClassName);
- }
- return null;
- }
- }
- else {
- if ( !typeOrClassName ) {
- cc.errorID(3804);
- return null;
- }
- constructor = typeOrClassName;
- }
-
- // check component
-
- if (typeof constructor !== 'function') {
- cc.errorID(3809);
- return null;
- }
- if (!cc.isChildClassOf(constructor, cc.Component)) {
- cc.errorID(3810);
- return null;
- }
-
- if (CC_EDITOR && constructor._disallowMultiple) {
- if (!this._checkMultipleComp(constructor)) {
- return null;
- }
- }
-
- // check requirement
-
- var ReqComp = constructor._requireComponent;
- if (ReqComp && !this.getComponent(ReqComp)) {
- var depended = this.addComponent(ReqComp);
- if (!depended) {
- // depend conflicts
- return null;
- }
- }
-
- //// check conflict
- //
- //if (CC_EDITOR && !_Scene.DetectConflict.beforeAddComponent(this, constructor)) {
- // return null;
- //}
-
- //
-
- var component = new constructor();
- component.node = this;
- this._components.push(component);
-
- if (this._activeInHierarchy) {
- if (typeof component.__preload === 'function') {
- cc.Component._callPreloadOnComponent(component);
- }
- // call onLoad/onEnable
- component.__onNodeActivated(true);
- }
-
- return component;
- },
-
- /**
- * This api should only used by undo system
- * @method _addComponentAt
- * @param {Component} comp
- * @param {Number} index
- * @private
- */
- _addComponentAt: CC_EDITOR && function (comp, index) {
- if (this._objFlags & Destroying) {
- return cc.error('isDestroying');
- }
- if ( !(comp instanceof cc.Component) ) {
- return cc.errorID(3811);
- }
- if (index > this._components.length) {
- return cc.errorID(3812);
- }
-
- // recheck attributes because script may changed
- var ctor = comp.constructor;
- if (ctor._disallowMultiple) {
- if (!this._checkMultipleComp(ctor)) {
- return;
- }
- }
- var ReqComp = ctor._requireComponent;
- if (ReqComp && !this.getComponent(ReqComp)) {
- if (index === this._components.length) {
- // If comp should be last component, increase the index because required component added
- ++index;
- }
- var depended = this.addComponent(ReqComp);
- if (!depended) {
- // depend conflicts
- return null;
- }
- }
-
- comp.node = this;
- this._components.splice(index, 0, comp);
-
- if (this._activeInHierarchy) {
- if (typeof comp.__preload === 'function' &&
- !(comp._objFlags & cc.Object.Flags.IsPreloadCalled)) {
- cc.Component._callPreloadOnComponent(comp);
- }
- // call onLoad/onEnable
- comp.__onNodeActivated(true);
- }
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 删除节点上的指定组件,传入参数可以是一个组件构造函数或组件名,也可以是已经获得的组件引用。
- * 如果你已经获得组件引用,你也可以直接调用 component.destroy()
- * @method removeComponent
- * @param {String|Function|Component} component - The need remove component.
- * @deprecated please destroy the component to remove it.
- * @example
- * node.removeComponent(cc.Sprite);
- * var Test = require("Test");
- * node.removeComponent(Test);
- */
- removeComponent: function (component) {
- if ( !component ) {
- cc.errorID(3813);
- return;
- }
- if (typeof component !== 'object') {
- component = this.getComponent(component);
- }
- if (component) {
- component.destroy();
- }
- },
-
- /**
- * @method _getDependComponent
- * @param {Component} depended
- * @return {Component}
- * @private
- */
- _getDependComponent: CC_EDITOR && function (depended) {
- for (var i = 0; i < this._components.length; i++) {
- var comp = this._components[i];
- if (comp !== depended && comp.isValid && !cc.Object._willDestroy(comp)) {
- var depend = comp.constructor._requireComponent;
- if (depend && depended instanceof depend) {
- return comp;
- }
- }
- }
- return null;
- },
-
- // do remove component, only used internally
- _removeComponent: function (component) {
- if (!component) {
- cc.errorID(3814);
- return;
- }
-
- if (!(this._objFlags & Destroying)) {
- var i = this._components.indexOf(component);
- if (i !== -1) {
- this._components.splice(i, 1);
- }
- else if (component.node !== this) {
- cc.errorID(3815);
- }
- }
- },
-
// INTERNAL
_registerIfAttached: (CC_EDITOR || CC_TEST) && function (register) {
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 79942e411d..dcfeea2db1 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -28,6 +28,7 @@ var Flags = require('../platform/CCObject').Flags;
var Misc = require('./misc');
var IdGenerater = require('../platform/id-generater');
+var JS = cc.js;
var Destroying = Flags.Destroying;
var DontDestroy = Flags.DontDestroy;
var Activating = Flags.Activating;
@@ -272,8 +273,11 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
},
- ctor: function () {
+ ctor: function (name) {
+ this._name = typeof name !== 'undefined' ? name : 'New Node';
+
+ this._activeInHierarchy = false;
// Support for ActionManager and EventManager
this.__instanceId = this._id || cc.ClassManager.getNewInstanceId();
@@ -468,6 +472,305 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return false;
},
+ // COMPONENT
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
+ * 传入参数也可以是脚本的名称。
+ * @method getComponent
+ * @param {Function|String} typeOrClassName
+ * @return {Component}
+ * @example
+ * // get sprite component.
+ * var sprite = node.getComponent(cc.Sprite);
+ * // get custom test calss.
+ * var test = node.getComponent("Test");
+ */
+ getComponent: function (typeOrClassName) {
+ var constructor = getConstructor(typeOrClassName);
+ if (constructor) {
+ return findComponent(this, constructor);
+ }
+ return null;
+ },
+
+ /**
+ * !#en Returns all components of supplied type in the node.
+ * !#zh 返回节点上指定类型的所有组件。
+ * @method getComponents
+ * @param {Function|String} typeOrClassName
+ * @return {Component[]}
+ * @example
+ * var sprites = node.getComponents(cc.Sprite);
+ * var tests = node.getComponents("Test");
+ */
+ getComponents: function (typeOrClassName) {
+ var constructor = getConstructor(typeOrClassName), components = [];
+ if (constructor) {
+ findComponents(this, constructor, components);
+ }
+ return components;
+ },
+
+ /**
+ * !#en Returns the component of supplied type in any of its children using depth first search.
+ * !#zh 递归查找所有子节点中第一个匹配指定类型的组件。
+ * @method getComponentInChildren
+ * @param {Function|String} typeOrClassName
+ * @return {Component}
+ * @example
+ * var sprite = node.getComponentInChildren(cc.Sprite);
+ * var Test = node.getComponentInChildren("Test");
+ */
+ getComponentInChildren: function (typeOrClassName) {
+ var constructor = getConstructor(typeOrClassName);
+ if (constructor) {
+ return findChildComponent(this._children, constructor);
+ }
+ return null;
+ },
+
+ /**
+ * !#en Returns all components of supplied type in self or any of its children.
+ * !#zh 递归查找自身或所有子节点中指定类型的组件
+ * @method getComponentsInChildren
+ * @param {Function|String} typeOrClassName
+ * @return {Component[]}
+ * @example
+ * var sprites = node.getComponentsInChildren(cc.Sprite);
+ * var tests = node.getComponentsInChildren("Test");
+ */
+ getComponentsInChildren: function (typeOrClassName) {
+ var constructor = getConstructor(typeOrClassName), components = [];
+ if (constructor) {
+ findComponents(this, constructor, components);
+ findChildComponents(this._children, constructor, components);
+ }
+ return components;
+ },
+
+ _checkMultipleComp: CC_EDITOR && function (ctor) {
+ var existing = this.getComponent(ctor._disallowMultiple);
+ if (existing) {
+ if (existing.constructor === ctor) {
+ cc.errorID(3805, JS.getClassName(ctor), this._name);
+ }
+ else {
+ cc.errorID(3806, JS.getClassName(ctor), this._name, JS.getClassName(existing));
+ }
+ return false;
+ }
+ return true;
+ },
+
+ /**
+ * !#en Adds a component class to the node. You can also add component to node by passing in the name of the script.
+ * !#zh 向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。
+ * @method addComponent
+ * @param {Function|String} typeOrClassName - The constructor or the class name of the component to add
+ * @return {Component} - The newly added component
+ * @example
+ * var sprite = node.addComponent(cc.Sprite);
+ * var test = node.addComponent("Test");
+ */
+ addComponent: function (typeOrClassName) {
+
+ if (CC_EDITOR && (this._objFlags & Destroying)) {
+ cc.error('isDestroying');
+ return null;
+ }
+
+ // get component
+
+ var constructor;
+ if (typeof typeOrClassName === 'string') {
+ constructor = JS.getClassByName(typeOrClassName);
+ if ( !constructor ) {
+ cc.errorID(3807, typeOrClassName);
+ if (cc._RFpeek()) {
+ cc.errorID(3808, typeOrClassName);
+ }
+ return null;
+ }
+ }
+ else {
+ if ( !typeOrClassName ) {
+ cc.errorID(3804);
+ return null;
+ }
+ constructor = typeOrClassName;
+ }
+
+ // check component
+
+ if (typeof constructor !== 'function') {
+ cc.errorID(3809);
+ return null;
+ }
+ if (!cc.isChildClassOf(constructor, cc.Component)) {
+ cc.errorID(3810);
+ return null;
+ }
+
+ if (CC_EDITOR && constructor._disallowMultiple) {
+ if (!this._checkMultipleComp(constructor)) {
+ return null;
+ }
+ }
+
+ // check requirement
+
+ var ReqComp = constructor._requireComponent;
+ if (ReqComp && !this.getComponent(ReqComp)) {
+ var depended = this.addComponent(ReqComp);
+ if (!depended) {
+ // depend conflicts
+ return null;
+ }
+ }
+
+ //// check conflict
+ //
+ //if (CC_EDITOR && !_Scene.DetectConflict.beforeAddComponent(this, constructor)) {
+ // return null;
+ //}
+
+ //
+
+ var component = new constructor();
+ component.node = this;
+ this._components.push(component);
+
+ if (this._activeInHierarchy) {
+ if (typeof component.__preload === 'function') {
+ cc.Component._callPreloadOnComponent(component);
+ }
+ // call onLoad/onEnable
+ component.__onNodeActivated(true);
+ }
+
+ return component;
+ },
+
+ /**
+ * This api should only used by undo system
+ * @method _addComponentAt
+ * @param {Component} comp
+ * @param {Number} index
+ * @private
+ */
+ _addComponentAt: CC_EDITOR && function (comp, index) {
+ if (this._objFlags & Destroying) {
+ return cc.error('isDestroying');
+ }
+ if ( !(comp instanceof cc.Component) ) {
+ return cc.errorID(3811);
+ }
+ if (index > this._components.length) {
+ return cc.errorID(3812);
+ }
+
+ // recheck attributes because script may changed
+ var ctor = comp.constructor;
+ if (ctor._disallowMultiple) {
+ if (!this._checkMultipleComp(ctor)) {
+ return;
+ }
+ }
+ var ReqComp = ctor._requireComponent;
+ if (ReqComp && !this.getComponent(ReqComp)) {
+ if (index === this._components.length) {
+ // If comp should be last component, increase the index because required component added
+ ++index;
+ }
+ var depended = this.addComponent(ReqComp);
+ if (!depended) {
+ // depend conflicts
+ return null;
+ }
+ }
+
+ comp.node = this;
+ this._components.splice(index, 0, comp);
+
+ if (this._activeInHierarchy) {
+ if (typeof comp.__preload === 'function' &&
+ !(comp._objFlags & cc.Object.Flags.IsPreloadCalled)) {
+ cc.Component._callPreloadOnComponent(comp);
+ }
+ // call onLoad/onEnable
+ comp.__onNodeActivated(true);
+ }
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 删除节点上的指定组件,传入参数可以是一个组件构造函数或组件名,也可以是已经获得的组件引用。
+ * 如果你已经获得组件引用,你也可以直接调用 component.destroy()
+ * @method removeComponent
+ * @param {String|Function|Component} component - The need remove component.
+ * @deprecated please destroy the component to remove it.
+ * @example
+ * node.removeComponent(cc.Sprite);
+ * var Test = require("Test");
+ * node.removeComponent(Test);
+ */
+ removeComponent: function (component) {
+ if ( !component ) {
+ cc.errorID(3813);
+ return;
+ }
+ if (typeof component !== 'object') {
+ component = this.getComponent(component);
+ }
+ if (component) {
+ component.destroy();
+ }
+ },
+
+ /**
+ * @method _getDependComponent
+ * @param {Component} depended
+ * @return {Component}
+ * @private
+ */
+ _getDependComponent: CC_EDITOR && function (depended) {
+ for (var i = 0; i < this._components.length; i++) {
+ var comp = this._components[i];
+ if (comp !== depended && comp.isValid && !cc.Object._willDestroy(comp)) {
+ var depend = comp.constructor._requireComponent;
+ if (depend && depended instanceof depend) {
+ return comp;
+ }
+ }
+ }
+ return null;
+ },
+
+ // do remove component, only used internally
+ _removeComponent: function (component) {
+ if (!component) {
+ cc.errorID(3814);
+ return;
+ }
+
+ if (!(this._objFlags & Destroying)) {
+ var i = this._components.indexOf(component);
+ if (i !== -1) {
+ this._components.splice(i, 1);
+ }
+ else if (component.node !== this) {
+ cc.errorID(3815);
+ }
+ }
+ },
});
From c072de5ae54eb3f7550caf1e7d2757de978ec97d Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Mon, 26 Dec 2016 16:35:56 +0800
Subject: [PATCH 22/36] remove unused local functions
---
cocos2d/core/CCNode.js | 46 ------------------------------------------
1 file changed, 46 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 4b214afac4..7f10bf14e3 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -278,52 +278,6 @@ function getConstructor (typeOrClassName) {
return typeOrClassName;
}
-function findComponent (node, constructor) {
- for (var i = 0; i < node._components.length; ++i) {
- var comp = node._components[i];
- if (comp instanceof constructor) {
- return comp;
- }
- }
- return null;
-}
-
-function findComponents (node, constructor, components) {
- for (var i = 0; i < node._components.length; ++i) {
- var comp = node._components[i];
- if (comp instanceof constructor) {
- components.push(comp);
- }
- }
-}
-
-function findChildComponent (children, constructor) {
- for (var i = 0; i < children.length; ++i) {
- var node = children[i];
- var comp = findComponent(node, constructor);
- if (comp) {
- return comp;
- }
- else if (node.children.length > 0) {
- comp = findChildComponent(node.children, constructor);
- if (comp) {
- return comp;
- }
- }
- }
- return null;
-}
-
-function findChildComponents (children, constructor, components) {
- for (var i = 0; i < children.length; ++i) {
- var node = children[i];
- findComponents(node, constructor, components);
- if (node._children.length > 0) {
- findChildComponents(node._children, constructor, components);
- }
- }
-}
-
/**
* !#en
* Class of all entities in Cocos Creator scenes.
From 73aa2e640fc9ad802250cd92aebfa7597de4a91e Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Tue, 27 Dec 2016 11:46:55 +0800
Subject: [PATCH 23/36] move _deactivateChildComponents to base node
---
cocos2d/core/CCNode.js | 16 ----------------
cocos2d/core/utils/base-node.js | 22 +++++++++++++++++-----
2 files changed, 17 insertions(+), 21 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 7f10bf14e3..2bc74f1e64 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -1246,22 +1246,6 @@ var Node = cc.Class({
}
},
- _deactivateChildComponents: function () {
- // 和 _activeRecursively 类似但不修改 this._activeInHierarchy
- var originCount = this._components.length;
- for (var c = 0; c < originCount; ++c) {
- var component = this._components[c];
- component.__onNodeActivated(false);
- }
- // deactivate children recursively
- for (var i = 0, len = this.childrenCount; i < len; ++i) {
- var entity = this._children[i];
- if (entity._active) {
- entity._deactivateChildComponents();
- }
- }
- },
-
_instantiate: function (cloned) {
if (!cloned) {
cloned = cc.instantiate._clone(this, this);
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index dcfeea2db1..ea2916d2d1 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -418,11 +418,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
removeChildByTag: null,
removeAllChildren: null,
- getNodeToParentTransform: null,
- getNodeToWorldTransform: null,
- getParentToNodeTransform: null,
- getWorldToNodeTransform: null,
-
// HIERARCHY METHODS
/**
@@ -771,6 +766,23 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
}
},
+
+ _deactivateChildComponents: function () {
+ // 和 _activeRecursively 类似但不修改 this._activeInHierarchy
+ var originCount = this._components.length;
+ for (var c = 0; c < originCount; ++c) {
+ var component = this._components[c];
+ component.__onNodeActivated(false);
+ }
+ // deactivate children recursively
+ for (var i = 0, len = this.childrenCount; i < len; ++i) {
+ var entity = this._children[i];
+ if (entity._active) {
+ entity._deactivateChildComponents();
+ }
+ }
+ },
+
});
From b75f8ffcf3bd6a8a15e4cbe5d2c1e7f3ace5489b Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Tue, 27 Dec 2016 14:54:04 +0800
Subject: [PATCH 24/36] move active properties and some related functions to
base node
---
cocos2d/core/CCNode.js | 123 +-------------------------------
cocos2d/core/utils/base-node.js | 119 ++++++++++++++++++++++++++++++
2 files changed, 121 insertions(+), 121 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 2bc74f1e64..1d834a089b 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -311,52 +311,6 @@ var Node = cc.Class({
_globalZOrder: 0,
_opacityModifyRGB: false,
- /**
- * !#en
- * 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 {{#crossLink "Node/activeInHierarchy:property"}}{{/crossLink}} if you want to check if the Node is actually treated as active in the scene.
- * !#zh
- * 当前节点的自身激活状态。
- * 值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。
- * 如果你想检查节点在场景中实际的激活状态可以使用 {{#crossLink "Node/activeInHierarchy:property"}}{{/crossLink}}。
- * @property active
- * @type {Boolean}
- * @default true
- * @example
- * node.active = false;
- */
- active: {
- get: function () {
- return this._active;
- },
- set: function (value) {
- value = !!value;
- if (this._active !== value) {
- this._active = value;
- var couldActiveInHierarchy = (this._parent && this._parent._activeInHierarchy);
- if (couldActiveInHierarchy) {
- this._onActivatedInHierarchy(value);
- this.emit('active-in-hierarchy-changed', this);
- }
- }
- }
- },
-
- /**
- * !#en Indicates whether this node is active in the scene.
- * !#zh 表示此节点是否在场景中激活。
- * @property activeInHierarchy
- * @type {Boolean}
- * @example
- * cc.log("activeInHierarchy: " + node.activeInHierarchy);
- */
- activeInHierarchy: {
- get: function () {
- return this._activeInHierarchy;
- }
- },
-
// internal properties
/**
@@ -1103,71 +1057,8 @@ var Node = cc.Class({
}
},
- _activeRecursively: function (newActive) {
- var cancelActivation = false;
- if (this._objFlags & Activating) {
- if (newActive) {
- cc.errorID(3816, this.name);
- return;
- }
- else {
- cancelActivation = true;
- }
- }
- else if (newActive) {
- this._objFlags |= Activating;
- }
-
- this._activeInHierarchy = newActive;
-
- // component maybe added during onEnable, and the onEnable of new component is already called
- // so we should record the origin length
- var originCount = this._components.length;
- for (var c = 0; c < originCount; ++c) {
- var component = this._components[c];
- if (component instanceof cc.Component) {
- component.__onNodeActivated(newActive);
- if (newActive && !this._activeInHierarchy) {
- // deactivated during activating
- this._objFlags &= ~Activating;
- return;
- }
- }
- else {
- if (CC_DEV) {
- cc.errorID(3817, this.name, c);
- console.log('Corrupted component value:', component);
- }
- if (component) {
- this._removeComponent(component);
- }
- else {
- JS.array.removeAt(this._components, c);
- }
- --c;
- --originCount;
- }
- }
-
- // activate children recursively
- for (var i = 0, len = this._children.length; i < len; ++i) {
- var child = this._children[i];
- if (child._active) {
- child._activeRecursively(newActive);
- if (newActive && !this._activeInHierarchy) {
- // deactivated during activating
- this._objFlags &= ~Activating;
- return;
- }
- }
- }
-
- if (cancelActivation) {
- this._objFlags &= ~Activating;
- return;
- }
-
- // ActionManager, EventManager
+ _onActive_EventsActions: function (newActive) {
+// ActionManager, EventManager
if (newActive) {
// activate
cc.director.getActionManager().resumeTarget(this);
@@ -1184,16 +1075,6 @@ var Node = cc.Class({
cc.director.getActionManager().pauseTarget(this);
cc.eventManager.pauseTarget(this);
}
-
- //
- this._objFlags &= ~Activating;
- },
-
- _onActivatedInHierarchy: function (newActive) {
- if (newActive) {
- cc.Component._callPreloadOnNode(this);
- }
- this._activeRecursively(newActive);
},
_onHierarchyChanged: function (oldParent) {
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index ea2916d2d1..badf8e2a51 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -262,6 +262,52 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
+ /**
+ * !#en
+ * 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 {{#crossLink "Node/activeInHierarchy:property"}}{{/crossLink}} if you want to check if the Node is actually treated as active in the scene.
+ * !#zh
+ * 当前节点的自身激活状态。
+ * 值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。
+ * 如果你想检查节点在场景中实际的激活状态可以使用 {{#crossLink "Node/activeInHierarchy:property"}}{{/crossLink}}。
+ * @property active
+ * @type {Boolean}
+ * @default true
+ * @example
+ * node.active = false;
+ */
+ active: {
+ get: function () {
+ return this._active;
+ },
+ set: function (value) {
+ value = !!value;
+ if (this._active !== value) {
+ this._active = value;
+ var couldActiveInHierarchy = (this._parent && this._parent._activeInHierarchy);
+ if (couldActiveInHierarchy) {
+ this._onActivatedInHierarchy(value);
+ this.emit('active-in-hierarchy-changed', this);
+ }
+ }
+ }
+ },
+
+ /**
+ * !#en Indicates whether this node is active in the scene.
+ * !#zh 表示此节点是否在场景中激活。
+ * @property activeInHierarchy
+ * @type {Boolean}
+ * @example
+ * cc.log("activeInHierarchy: " + node.activeInHierarchy);
+ */
+ activeInHierarchy: {
+ get: function () {
+ return this._activeInHierarchy;
+ }
+ },
+
/**
* !#en Tag of node.
* !#zh 节点标签。
@@ -767,6 +813,79 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
+ _onActivatedInHierarchy: function (newActive) {
+ if (newActive) {
+ cc.Component._callPreloadOnNode(this);
+ }
+ this._activeRecursively(newActive);
+ },
+
+ _activeRecursively: function (newActive) {
+ var cancelActivation = false;
+ if (this._objFlags & Activating) {
+ if (newActive) {
+ cc.errorID(3816, this.name);
+ return;
+ }
+ else {
+ cancelActivation = true;
+ }
+ }
+ else if (newActive) {
+ this._objFlags |= Activating;
+ }
+
+ this._activeInHierarchy = newActive;
+
+ // component maybe added during onEnable, and the onEnable of new component is already called
+ // so we should record the origin length
+ var originCount = this._components.length;
+ for (var c = 0; c < originCount; ++c) {
+ var component = this._components[c];
+ if (component instanceof cc.Component) {
+ component.__onNodeActivated(newActive);
+ if (newActive && !this._activeInHierarchy) {
+ // deactivated during activating
+ this._objFlags &= ~Activating;
+ return;
+ }
+ }
+ else {
+ if (CC_DEV) {
+ cc.errorID(3817, this.name, c);
+ console.log('Corrupted component value:', component);
+ }
+ if (component) {
+ this._removeComponent(component);
+ }
+ else {
+ JS.array.removeAt(this._components, c);
+ }
+ --c;
+ --originCount;
+ }
+ }
+
+ // activate children recursively
+ for (var i = 0, len = this._children.length; i < len; ++i) {
+ var child = this._children[i];
+ if (child._active) {
+ child._activeRecursively(newActive);
+ if (newActive && !this._activeInHierarchy) {
+ // deactivated during activating
+ this._objFlags &= ~Activating;
+ return;
+ }
+ }
+ }
+ this._objFlags &= ~Activating;
+
+ if (!cancelActivation && this._onActive_EventsActions) {
+ this._onActive_EventsActions(newActive);
+ }
+
+ },
+
_deactivateChildComponents: function () {
// 和 _activeRecursively 类似但不修改 this._activeInHierarchy
var originCount = this._components.length;
From 0add0fc83a35372fea110e68ad264d66c5665e7c Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Tue, 27 Dec 2016 18:27:57 +0800
Subject: [PATCH 25/36] refactor and move base functions to base node
---
cocos2d/core/CCNode.js | 226 +------------------
cocos2d/core/utils/base-node.js | 371 +++++++++++++++++++++++++++++++-
2 files changed, 363 insertions(+), 234 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 1d834a089b..555536c652 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -266,18 +266,6 @@ var _searchMaskParent = function (node) {
return null;
};
-function getConstructor (typeOrClassName) {
- if ( !typeOrClassName ) {
- cc.errorID(3804);
- return null;
- }
- if (typeof typeOrClassName === 'string') {
- return JS.getClassByName(typeOrClassName);
- }
-
- return typeOrClassName;
-}
-
/**
* !#en
* Class of all entities in Cocos Creator scenes.
@@ -913,15 +901,6 @@ var Node = cc.Class({
// OVERRIDES
- destroy: function () {
- if (cc.Object.prototype.destroy.call(this)) {
- // disable hierarchy
- if (this._activeInHierarchy) {
- this._deactivateChildComponents();
- }
- }
- },
-
_onPreDestroy: function () {
var i, len;
@@ -1010,23 +989,6 @@ var Node = cc.Class({
// INTERNAL
- _registerIfAttached: (CC_EDITOR || CC_TEST) && function (register) {
- if (register) {
- cc.engine.attachedObjsForEditor[this.uuid] = this;
- cc.engine.emit('node-attach-to-scene', {target: this});
- //this._objFlags |= RegisteredInEditor;
- }
- else {
- cc.engine.emit('node-detach-from-scene', {target: this});
- delete cc.engine.attachedObjsForEditor[this._id];
- }
- var children = this._children;
- for (var i = 0, len = children.length; i < len; ++i) {
- var child = children[i];
- child._registerIfAttached(register);
- }
- },
-
/*
* The initializer for Node which will be called before all components onLoad
*/
@@ -1078,78 +1040,8 @@ var Node = cc.Class({
},
_onHierarchyChanged: function (oldParent) {
- var newParent = this._parent;
- if (this._persistNode && !(newParent instanceof cc.Scene)) {
- cc.game.removePersistRootNode(this);
- if (CC_EDITOR) {
- cc.warnID(1623);
- }
- }
- var activeInHierarchyBefore = this._active && !!(oldParent && oldParent._activeInHierarchy);
- var shouldActiveNow = this._active && !!(newParent && newParent._activeInHierarchy);
- if (activeInHierarchyBefore !== shouldActiveNow) {
- this._onActivatedInHierarchy(shouldActiveNow);
- }
+ this._onHierarchyChangedBase(oldParent);
cc._widgetManager._nodesOrderDirty = true;
- if (CC_EDITOR || CC_TEST) {
- var scene = cc.director.getScene();
- var inCurrentSceneBefore = oldParent && oldParent.isChildOf(scene);
- var inCurrentSceneNow = newParent && newParent.isChildOf(scene);
- if (!inCurrentSceneBefore && inCurrentSceneNow) {
- // attached
- this._registerIfAttached(true);
- }
- else if (inCurrentSceneBefore && !inCurrentSceneNow) {
- // detached
- this._registerIfAttached(false);
- }
-
- // update prefab
- var newPrefabRoot = newParent && newParent._prefab && newParent._prefab.root;
- var myPrefabInfo = this._prefab;
- if (myPrefabInfo) {
- if (newPrefabRoot) {
- // change prefab
- _Scene.PrefabUtils.linkPrefab(newPrefabRoot._prefab.asset, newPrefabRoot, this);
- }
- else if (myPrefabInfo.root !== this) {
- // detach from prefab
- _Scene.PrefabUtils.unlinkPrefab(this);
- }
- }
- else if (newPrefabRoot) {
- // attach to prefab
- _Scene.PrefabUtils.linkPrefab(newPrefabRoot._prefab.asset, newPrefabRoot, this);
- }
-
- // conflict detection
- _Scene.DetectConflict.afterAddChild(this);
- }
- },
-
- _instantiate: function (cloned) {
- if (!cloned) {
- cloned = cc.instantiate._clone(this, this);
- }
-
- var thisPrefabInfo = this._prefab;
- var syncing = thisPrefabInfo && this === thisPrefabInfo.root && thisPrefabInfo.sync;
- if (syncing) {
- // copy non-serialized property
- cloned._prefab._synced = thisPrefabInfo._synced;
- //if (thisPrefabInfo._synced) {
- // return clone;
- //}
- }
- else if (CC_EDITOR && cc.engine._isPlaying) {
- cloned._name += ' (Clone)';
- }
-
- // reset and init
- cloned._parent = null;
- cloned._onBatchCreated();
-
- return cloned;
},
// EVENTS
@@ -2427,122 +2319,6 @@ var Node = cc.Class({
}
},
- // composition: REMOVE
-
- /**
- * !#en
- * Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks.
- * If the cleanup parameter is not passed, it will force a cleanup.
- * If the node orphan, then nothing happens.
- * !#zh
- * 从父节点中删除一个节点。cleanup 参数为 true,那么在这个节点上所有的动作和回调都会被删除,反之则不会。
- * 如果不传入 cleanup 参数,默认是 true 的。
- * 如果这个节点是一个孤节点,那么什么都不会发生。
- * @method removeFromParent
- * @param {Boolean} [cleanup=true] - true if all actions and callbacks on this node should be removed, false otherwise.
- * @see cc.Node#removeFromParentAndCleanup
- * @example
- * node.removeFromParent();
- * node.removeFromParent(false);
- */
- removeFromParent: function (cleanup) {
- if (this._parent) {
- if (cleanup === undefined)
- cleanup = true;
- this._parent.removeChild(this, cleanup);
- }
- },
-
- /**
- * !#en
- * Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
- * If the cleanup parameter is not passed, it will force a cleanup.
- * "remove" logic MUST only be on this method
- * If a class wants to extend the 'removeChild' behavior it only needs
- * to override this method.
- * !#zh
- * 移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
- * 如果 cleanup 参数不传入,默认为 true 表示清理。
- * @method removeChild
- * @param {Node} child - The child node which will be removed.
- * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
- * @example
- * node.removeChild(newNode);
- * node.removeChild(newNode, false);
- */
- removeChild: function (child, cleanup) {
- if (this._children.indexOf(child) > -1) {
- // If you don't do cleanup, the child's actions will not get removed and the
- if (cleanup || cleanup === undefined) {
- child.cleanup();
- }
- // invoke the parent setter
- child.parent = null;
- }
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 通过标签移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
- * 如果 cleanup 参数不传入,默认为 true 表示清理。
- * @method removeChildByTag
- * @param {Number} tag - An integer number that identifies a child node
- * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
- * @see cc.Node#removeChildByTag
- * @example
- * node.removeChildByTag(1001);
- * node.removeChildByTag(1001, false);
- */
- removeChildByTag: function (tag, cleanup) {
- if (tag === cc.macro.NODE_TAG_INVALID)
- cc.logID(1609);
-
- var child = this.getChildByTag(tag);
- if (!child)
- cc.logID(1610, tag);
- else
- this.removeChild(child, cleanup);
- },
-
- /**
- * !#en
- * 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.
- * !#zh
- * 移除节点所有的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
- * 如果 cleanup 参数不传入,默认为 true 表示清理。
- * @method removeAllChildren
- * @param {Boolean} [cleanup=true] - true if all running actions on all children nodes should be cleanup, false otherwise.
- * @example
- * node.removeAllChildren();
- * node.removeAllChildren(false);
- */
- removeAllChildren: function (cleanup) {
- // not using detachChild improves speed here
- var children = this._children;
- if (cleanup === undefined)
- cleanup = true;
- for (var i = children.length - 1; i >= 0; i--) {
- var node = children[i];
- if (node) {
- //if (this._running) {
- // node.onExitTransitionDidStart();
- // node.onExit();
- //}
-
- // If you don't do cleanup, the node's actions will not get removed and the
- if (cleanup)
- node.cleanup();
-
- node.parent = null;
- }
- }
- this._children.length = 0;
- },
-
/**
* !#en Sorts the children array depends on children's zIndex and arrivalOrder,
* normally you won't need to invoke this function.
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index badf8e2a51..52ce162c06 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -349,8 +349,40 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
return this._parent;
},
- //interface need to be implemented in derived classes
- setParent: null,
+ setParent: function (value) {
+ if (this._parent === value) {
+ return;
+ }
+ if (CC_EDITOR && !cc.engine.isPlaying) {
+ if (_Scene.DetectConflict.beforeAddChild(this)) {
+ return;
+ }
+ }
+ //
+ var oldParent = this._parent;
+ this._parent = value || null;
+ if (value) {
+ if (!CC_JSB) {
+ cc.eventManager._setDirtyForNode(this);
+ }
+ value._children.push(this);
+ value.emit(CHILD_ADDED, this);
+ }
+ if (oldParent) {
+ if (!(oldParent._objFlags & Destroying)) {
+ var removeAt = oldParent._children.indexOf(this);
+ if (CC_DEV && removeAt < 0) {
+ return cc.errorID(1633);
+ }
+ oldParent._children.splice(removeAt, 1);
+ oldParent.emit(CHILD_REMOVED, this);
+ this._onHierarchyChanged(oldParent);
+ }
+ }
+ else if (value) {
+ this._onHierarchyChanged(null);
+ }
+ },
// ABSTRACT INTERFACES
@@ -458,11 +490,18 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
},
// composition: ADD
- addChild: null,
- removeFromParent: null,
- removeChild: null,
- removeChildByTag: null,
- removeAllChildren: null,
+ addChild: function (child) {
+
+ if (CC_DEV && !(child instanceof cc._BaseNode)) {
+ return cc.errorID(1634, cc.js.getClassName(child));
+ }
+ cc.assertID(child, 1606);
+ cc.assertID(child._parent === null, 1605);
+
+ // invokes the parent setter
+ child.setParent(this);
+
+ },
// HIERARCHY METHODS
@@ -491,7 +530,140 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* @example
* node.setSiblingIndex(1);
*/
- setSiblingIndex: null,
+ setSiblingIndex: function (index) {
+ if (!this._parent) {
+ return;
+ }
+ var array = this._parent._children;
+ index = index !== -1 ? index : array.length - 1;
+ var oldIndex = array.indexOf(this);
+ if (index !== oldIndex) {
+ array.splice(oldIndex, 1);
+ if (index < array.length) {
+ array.splice(index, 0, this);
+ }
+ else {
+ array.push(this);
+ }
+ }
+ },
+
+ cleanup: null,
+
+ /**
+ * !#en
+ * Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks.
+ * If the cleanup parameter is not passed, it will force a cleanup.
+ * If the node orphan, then nothing happens.
+ * !#zh
+ * 从父节点中删除一个节点。cleanup 参数为 true,那么在这个节点上所有的动作和回调都会被删除,反之则不会。
+ * 如果不传入 cleanup 参数,默认是 true 的。
+ * 如果这个节点是一个孤节点,那么什么都不会发生。
+ * @method removeFromParent
+ * @param {Boolean} [cleanup=true] - true if all actions and callbacks on this node should be removed, false otherwise.
+ * @see cc.Node#removeFromParentAndCleanup
+ * @example
+ * node.removeFromParent();
+ * node.removeFromParent(false);
+ */
+ removeFromParent: function (cleanup) {
+ if (this._parent) {
+ if (cleanup === undefined)
+ cleanup = true;
+ this._parent.removeChild(this, cleanup);
+ }
+ },
+
+ /**
+ * !#en
+ * Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
+ * If the cleanup parameter is not passed, it will force a cleanup.
+ * "remove" logic MUST only be on this method
+ * If a class wants to extend the 'removeChild' behavior it only needs
+ * to override this method.
+ * !#zh
+ * 移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
+ * 如果 cleanup 参数不传入,默认为 true 表示清理。
+ * @method removeChild
+ * @param {Node} child - The child node which will be removed.
+ * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
+ * @example
+ * node.removeChild(newNode);
+ * node.removeChild(newNode, false);
+ */
+ removeChild: function (child, cleanup) {
+ if (this._children.indexOf(child) > -1) {
+ // If you don't do cleanup, the child's actions will not get removed and the
+ if (cleanup || cleanup === undefined) {
+ child.cleanup();
+ }
+ // invoke the parent setter
+ child.parent = null;
+ }
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 通过标签移除节点中指定的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
+ * 如果 cleanup 参数不传入,默认为 true 表示清理。
+ * @method removeChildByTag
+ * @param {Number} tag - An integer number that identifies a child node
+ * @param {Boolean} [cleanup=true] - true if all running actions and callbacks on the child node will be cleanup, false otherwise.
+ * @see cc.Node#removeChildByTag
+ * @example
+ * node.removeChildByTag(1001);
+ * node.removeChildByTag(1001, false);
+ */
+ removeChildByTag: function (tag, cleanup) {
+ if (tag === cc.macro.NODE_TAG_INVALID)
+ cc.logID(1609);
+
+ var child = this.getChildByTag(tag);
+ if (!child)
+ cc.logID(1610, tag);
+ else
+ this.removeChild(child, cleanup);
+ },
+
+ /**
+ * !#en
+ * 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.
+ * !#zh
+ * 移除节点所有的子节点,是否需要清理所有正在运行的行为取决于 cleanup 参数。
+ * 如果 cleanup 参数不传入,默认为 true 表示清理。
+ * @method removeAllChildren
+ * @param {Boolean} [cleanup=true] - true if all running actions on all children nodes should be cleanup, false otherwise.
+ * @example
+ * node.removeAllChildren();
+ * node.removeAllChildren(false);
+ */
+ removeAllChildren: function (cleanup) {
+ // not using detachChild improves speed here
+ var children = this._children;
+ if (cleanup === undefined)
+ cleanup = true;
+ for (var i = children.length - 1; i >= 0; i--) {
+ var node = children[i];
+ if (node) {
+ //if (this._running) {
+ // node.onExitTransitionDidStart();
+ // node.onExit();
+ //}
+
+ // If you don't do cleanup, the node's actions will not get removed and the
+ if (cleanup)
+ node.cleanup();
+
+ node.parent = null;
+ }
+ }
+ this._children.length = 0;
+ },
+
/**
* !#en Is this node a child of the given node?
* !#zh 是否是指定节点的子节点?
@@ -902,6 +1074,187 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
+ destroy: function () {
+ if (cc.Object.prototype.destroy.call(this)) {
+ // disable hierarchy
+ if (this._activeInHierarchy) {
+ this._deactivateChildComponents();
+ }
+ }
+ },
+
+ _onHierarchyChanged: function (oldParent) {
+ var newParent = this._parent;
+ if (this._persistNode && !(newParent instanceof cc.Scene)) {
+ cc.game.removePersistRootNode(this);
+ if (CC_EDITOR) {
+ cc.warnID(1623);
+ }
+ }
+ var activeInHierarchyBefore = this._active && !!(oldParent && oldParent._activeInHierarchy);
+ var shouldActiveNow = this._active && !!(newParent && newParent._activeInHierarchy);
+ if (activeInHierarchyBefore !== shouldActiveNow) {
+ this._onActivatedInHierarchy(shouldActiveNow);
+ }
+ if (CC_EDITOR || CC_TEST) {
+ var scene = cc.director.getScene();
+ var inCurrentSceneBefore = oldParent && oldParent.isChildOf(scene);
+ var inCurrentSceneNow = newParent && newParent.isChildOf(scene);
+ if (!inCurrentSceneBefore && inCurrentSceneNow) {
+ // attached
+ this._registerIfAttached(true);
+ }
+ else if (inCurrentSceneBefore && !inCurrentSceneNow) {
+ // detached
+ this._registerIfAttached(false);
+ }
+
+ // update prefab
+ var newPrefabRoot = newParent && newParent._prefab && newParent._prefab.root;
+ var myPrefabInfo = this._prefab;
+ if (myPrefabInfo) {
+ if (newPrefabRoot) {
+ // change prefab
+ _Scene.PrefabUtils.linkPrefab(newPrefabRoot._prefab.asset, newPrefabRoot, this);
+ }
+ else if (myPrefabInfo.root !== this) {
+ // detach from prefab
+ _Scene.PrefabUtils.unlinkPrefab(this);
+ }
+ }
+ else if (newPrefabRoot) {
+ // attach to prefab
+ _Scene.PrefabUtils.linkPrefab(newPrefabRoot._prefab.asset, newPrefabRoot, this);
+ }
+
+ // conflict detection
+ _Scene.DetectConflict.afterAddChild(this);
+ }
+
+ },
+
+ _onBatchCreated: function () {
+ var prefabInfo = this._prefab;
+ if (prefabInfo && prefabInfo.sync && !prefabInfo._synced) {
+ // checks to ensure no recursion, recursion will caused only on old data.
+ if (prefabInfo.root === this) {
+ PrefabHelper.syncWithPrefab(this);
+ }
+ }
+
+ var children = this._children;
+ for (var i = 0, len = children.length; i < len; i++) {
+ children[i]._onBatchCreated();
+ }
+ },
+
+ _instantiate: function (cloned) {
+ if (!cloned) {
+ cloned = cc.instantiate._clone(this, this);
+ }
+
+ var thisPrefabInfo = this._prefab;
+ var syncing = thisPrefabInfo && this === thisPrefabInfo.root && thisPrefabInfo.sync;
+ if (syncing) {
+ // copy non-serialized property
+ cloned._prefab._synced = thisPrefabInfo._synced;
+ //if (thisPrefabInfo._synced) {
+ // return clone;
+ //}
+ }
+ else if (CC_EDITOR && cc.engine._isPlaying) {
+ cloned._name += ' (Clone)';
+ }
+
+ // reset and init
+ cloned._parent = null;
+ cloned._onBatchCreated();
+
+ return cloned;
+ },
+
+ _registerIfAttached: (CC_EDITOR || CC_TEST) && function (register) {
+ if (register) {
+ cc.engine.attachedObjsForEditor[this.uuid] = this;
+ cc.engine.emit('node-attach-to-scene', {target: this});
+ //this._objFlags |= RegisteredInEditor;
+ }
+ else {
+ cc.engine.emit('node-detach-from-scene', {target: this});
+ delete cc.engine.attachedObjsForEditor[this._id];
+ }
+ var children = this._children;
+ for (var i = 0, len = children.length; i < len; ++i) {
+ var child = children[i];
+ child._registerIfAttached(register);
+ }
+ },
+
+ _onPreDestroy: function () {
+ var i, len;
+
+ // marked as destroying
+ this._objFlags |= Destroying;
+
+ // detach self and children from editor
+ var parent = this._parent;
+ var destroyByParent = parent && (parent._objFlags & Destroying);
+ if ( !destroyByParent ) {
+ if (CC_EDITOR || CC_TEST) {
+ this._registerIfAttached(false);
+ }
+ }
+
+ // destroy children
+ var children = this._children;
+ for (i = 0, len = children.length; i < len; ++i) {
+ // destroy immediate so its _onPreDestroy can be called
+ children[i]._destroyImmediate();
+ }
+
+ // destroy self components
+ for (i = 0, len = this._components.length; i < len; ++i) {
+ var component = this._components[i];
+ // destroy immediate so its _onPreDestroy can be called
+ component._destroyImmediate();
+ }
+
+
+ for (i = 0, len = this.__eventTargets.length; i < len; ++i) {
+ var target = this.__eventTargets[i];
+ target && target.targetOff(this);
+ }
+ this.__eventTargets.length = 0;
+
+ // remove from persist
+ if (this._persistNode) {
+ cc.game.removePersistRootNode(this);
+ }
+
+ if ( !destroyByParent ) {
+ // remove from parent
+ if (parent) {
+ var childIndex = parent._children.indexOf(this);
+ parent._children.splice(childIndex, 1);
+ parent.emit('child-removed', this);
+ }
+
+ // simulate some destruct logic to make undo system work correctly
+ if (CC_EDITOR) {
+ // ensure this node can reattach to scene by undo system
+ this._parent = null;
+ }
+ }
+ },
+
+ onRestore: CC_EDITOR && function () {
+ // check activity state
+ var shouldActiveInHierarchy = (this._parent && this._parent._activeInHierarchy && this._active);
+ if (shouldActiveInHierarchy !== this._activeInHierarchy) {
+ this._onActivatedInHierarchy(shouldActiveInHierarchy);
+ this.emit('active-in-hierarchy-changed', this);
+ }
+ },
});
@@ -916,5 +1269,5 @@ var DiffNameGetSets = {
//anchorY: ['_getAnchorY', '_setAnchorY'],
};
Misc.propertyDefine(BaseNode, SameNameGetSets, DiffNameGetSets);
-
+BaseNode.prototype._onHierarchyChangedBase = BaseNode.prototype._onHierarchyChanged;
cc._BaseNode = module.exports = BaseNode;
From 1dee140209d81b0362d73257b1bac0cc9e47bc78 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Wed, 28 Dec 2016 10:15:11 +0800
Subject: [PATCH 26/36] clean up code
---
cocos2d/core/CCNode.js | 3 ---
cocos2d/core/utils/base-node.js | 20 +++++++-------------
2 files changed, 7 insertions(+), 16 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 555536c652..5747fce281 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -27,11 +27,8 @@
var PrefabHelper = require('./utils/prefab-helper');
var SgHelper = require('./utils/scene-graph-helper');
-var JS = cc.js;
var Flags = cc.Object.Flags;
var Destroying = Flags.Destroying;
-var DontDestroy = Flags.DontDestroy;
-var Activating = Flags.Activating;
var POSITION_CHANGED = 'position-changed';
var SIZE_CHANGED = 'size-changed';
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 52ce162c06..7f3ea9b636 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -23,7 +23,6 @@
THE SOFTWARE.
****************************************************************************/
-var SgHelper = require('./scene-graph-helper');
var Flags = require('../platform/CCObject').Flags;
var Misc = require('./misc');
var IdGenerater = require('../platform/id-generater');
@@ -32,14 +31,10 @@ var JS = cc.js;
var Destroying = Flags.Destroying;
var DontDestroy = Flags.DontDestroy;
var Activating = Flags.Activating;
-var POSITION_CHANGED = 'position-changed';
-var SIZE_CHANGED = 'size-changed';
-var ANCHOR_CHANGED = 'anchor-changed';
+
var CHILD_ADDED = 'child-added';
var CHILD_REMOVED = 'child-removed';
-var CHILD_REORDER = 'child-reorder';
-var ERR_INVALID_NUMBER = CC_EDITOR && 'The %s is invalid';
var idGenerater = new IdGenerater('Node');
@@ -81,8 +76,8 @@ function findChildComponent (children, constructor) {
if (comp) {
return comp;
}
- else if (node.children.length > 0) {
- comp = findChildComponent(node.children, constructor);
+ else if (node._children.length > 0) {
+ comp = findChildComponent(node._children, constructor);
if (comp) {
return comp;
}
@@ -387,10 +382,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
// ABSTRACT INTERFACES
- // called when the node's parent changed
- _onHierarchyChanged: null,
- _onPreDestroy: null,
/*
* Initializes the instance of cc.Node
* @method init
@@ -548,7 +540,9 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- cleanup: null,
+ cleanup: function() {
+
+ },
/**
* !#en
@@ -1066,7 +1060,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
component.__onNodeActivated(false);
}
// deactivate children recursively
- for (var i = 0, len = this.childrenCount; i < len; ++i) {
+ for (var i = 0, len = this._children.length; i < len; ++i) {
var entity = this._children[i];
if (entity._active) {
entity._deactivateChildComponents();
From b7e441fcef1d8fcab0100d4c2945eabe7cdf0c2f Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Wed, 28 Dec 2016 10:45:19 +0800
Subject: [PATCH 27/36] migrate commit 1b841bb31ffecad3da91bcfa77f833ec8880e286
---
cocos2d/core/utils/base-node.js | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 7f3ea9b636..c76309a2c1 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -1148,6 +1148,11 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
var thisPrefabInfo = this._prefab;
+ if (CC_EDITOR && thisPrefabInfo) {
+ if (this !== thisPrefabInfo.root) {
+ _Scene.PrefabUtils.initClonedChildOfPrefab(cloned);
+ }
+ }
var syncing = thisPrefabInfo && this === thisPrefabInfo.root && thisPrefabInfo.sync;
if (syncing) {
// copy non-serialized property
From 96239d933bd7bdebae81010ca9ed47fdb1c83c94 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 10:15:47 +0800
Subject: [PATCH 28/36] reformat code
---
cocos2d/core/CCNode.js | 57 +++++++++++++++----------------
cocos2d/core/utils/base-node.js | 59 ++++++++++++++++-----------------
2 files changed, 58 insertions(+), 58 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 5747fce281..e9c511819a 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -445,7 +445,7 @@ var Node = cc.Class({
return this._rotationX;
},
set: function (value) {
- if (this._rotationX !== value || this._rotationY !== value ) {
+ if (this._rotationX !== value || this._rotationY !== value) {
this._rotationX = this._rotationY = value;
this._sgNode.rotation = value;
}
@@ -641,7 +641,7 @@ var Node = cc.Class({
return new cc.Color(color.r, color.g, color.b, color.a);
},
set: function (value) {
- if ( !this._color.equals(value) ) {
+ if (!this._color.equals(value)) {
var color = this._color;
color.r = value.r;
color.g = value.g;
@@ -825,7 +825,7 @@ var Node = cc.Class({
this._localZOrder = value;
this._sgNode.zIndex = value;
- if(this._parent) {
+ if (this._parent) {
this._parent._delaySort();
if (!CC_JSB) {
cc.eventManager._setDirtyForNode(this);
@@ -907,7 +907,7 @@ var Node = cc.Class({
// detach self and children from editor
var parent = this._parent;
var destroyByParent = parent && (parent._objFlags & Destroying);
- if ( !destroyByParent ) {
+ if (!destroyByParent) {
if (CC_EDITOR || CC_TEST) {
this._registerIfAttached(false);
}
@@ -961,7 +961,7 @@ var Node = cc.Class({
cc.game.removePersistRootNode(this);
}
- if ( !destroyByParent ) {
+ if (!destroyByParent) {
// remove from parent
if (parent) {
var childIndex = parent._children.indexOf(this);
@@ -1004,7 +1004,7 @@ var Node = cc.Class({
this._parent._sgNode.addChild(this._sgNode);
}
- if ( !this._activeInHierarchy ) {
+ if (!this._activeInHierarchy) {
// deactivate ActionManager and EventManager by default
cc.director.getActionManager().pauseTarget(this);
cc.eventManager.pauseTarget(this);
@@ -1113,7 +1113,7 @@ var Node = cc.Class({
}
}
if (newAdded && !this._activeInHierarchy) {
- cc.director.getScheduler().schedule(function() {
+ cc.director.getScheduler().schedule(function () {
if (!this._activeInHierarchy) {
cc.eventManager.pauseTarget(this);
}
@@ -1230,7 +1230,8 @@ var Node = cc.Class({
if (listener && listener.mask) {
var mask = listener.mask;
var parent = this;
- for (var i = 0; parent && i < mask.index; ++i, parent = parent.parent) {}
+ for (var i = 0; parent && i < mask.index; ++i, parent = parent.parent) {
+ }
// find mask parent, should hit test it
if (parent === mask.node) {
var comp = parent.getComponent(cc.Mask);
@@ -1555,7 +1556,7 @@ var Node = cc.Class({
}
var locPosition = this._position;
- if(locPosition.x === xValue && locPosition.y === yValue) {
+ if (locPosition.x === xValue && locPosition.y === yValue) {
return;
}
@@ -1931,7 +1932,7 @@ var Node = cc.Class({
getNodeToParentTransformAR: function () {
var contentSize = this.getContentSize();
var mat = this._sgNode.getNodeToParentTransform();
- if ( !this._isSgTransformArToMe(contentSize) ) {
+ if (!this._isSgTransformArToMe(contentSize)) {
// see getNodeToWorldTransform
var tx = this._anchorPoint.x * contentSize.width;
var ty = this._anchorPoint.y * contentSize.height;
@@ -1953,7 +1954,7 @@ var Node = cc.Class({
*/
getBoundingBox: function () {
var size = this.getContentSize();
- var rect = cc.rect( 0, 0, size.width, size.height );
+ var rect = cc.rect(0, 0, size.width, size.height);
return cc._rectApplyAffineTransformIn(rect, this.getNodeToParentTransform());
},
@@ -1981,7 +1982,7 @@ var Node = cc.Class({
var size = this.getContentSize();
var width = size.width;
var height = size.height;
- var rect = cc.rect(- this._anchorPoint.x * width, - this._anchorPoint.y * height, width, height);
+ var rect = cc.rect(-this._anchorPoint.x * width, -this._anchorPoint.y * height, width, height);
var transAR = cc.affineTransformConcat(this.getNodeToParentTransformAR(), parentTransformAR);
cc._rectApplyAffineTransformIn(rect, transAR);
@@ -2017,8 +2018,8 @@ var Node = cc.Class({
var mat = this._sgNode.getNodeToParentTransform();
if (this._isSgTransformArToMe(contentSize)) {
// see getNodeToWorldTransform
- var tx = - this._anchorPoint.x * contentSize.width;
- var ty = - this._anchorPoint.y * contentSize.height;
+ var tx = -this._anchorPoint.x * contentSize.width;
+ var ty = -this._anchorPoint.y * contentSize.height;
var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
mat = cc.affineTransformConcatIn(offset, mat);
}
@@ -2046,8 +2047,8 @@ var Node = cc.Class({
// _sgNode.getNodeToWorldTransform is not anchor relative (AR), in this case,
// we should translate to bottem left to consistent with it
// see https://github.com/cocos-creator/engine/pull/391
- var tx = - this._anchorPoint.x * contentSize.width;
- var ty = - this._anchorPoint.y * contentSize.height;
+ var tx = -this._anchorPoint.x * contentSize.width;
+ var ty = -this._anchorPoint.y * contentSize.height;
var offset = cc.affineTransformMake(1, 0, 0, 1, tx, ty);
mat = cc.affineTransformConcatIn(offset, mat);
}
@@ -2075,7 +2076,7 @@ var Node = cc.Class({
}
var mat = this._sgNode.getNodeToWorldTransform();
- if ( !this._isSgTransformArToMe(contentSize) ) {
+ if (!this._isSgTransformArToMe(contentSize)) {
// see getNodeToWorldTransform
var tx = this._anchorPoint.x * contentSize.width;
var ty = this._anchorPoint.y * contentSize.height;
@@ -2248,7 +2249,7 @@ var Node = cc.Class({
return this.convertToNodeSpaceAR(touch.getLocation());
},
- setNodeDirty: function(){
+ setNodeDirty: function () {
this._sgNode.setNodeDirty();
},
@@ -2267,13 +2268,13 @@ var Node = cc.Class({
addChild: function (child, localZOrder, tag) {
localZOrder = localZOrder === undefined ? child._localZOrder : localZOrder;
var name, setTag = false;
- if(typeof tag === 'undefined'){
+ if (typeof tag === 'undefined') {
tag = undefined;
name = child._name;
- } else if(cc.js.isString(tag)){
+ } else if (cc.js.isString(tag)) {
name = tag;
tag = undefined;
- } else if(cc.js.isNumber(tag)){
+ } else if (cc.js.isNumber(tag)) {
setTag = true;
name = "";
}
@@ -2330,23 +2331,23 @@ var Node = cc.Class({
if (_children.length > 1) {
// insertion sort
var len = _children.length, i, j, child;
- for (i = 1; i < len; i++){
+ for (i = 1; i < len; i++) {
child = _children[i];
j = i - 1;
//continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
- while(j >= 0){
+ while (j >= 0) {
if (child._localZOrder < _children[j]._localZOrder) {
- _children[j+1] = _children[j];
+ _children[j + 1] = _children[j];
} else if (child._localZOrder === _children[j]._localZOrder &&
child._sgNode._arrivalOrder < _children[j]._sgNode._arrivalOrder) {
- _children[j+1] = _children[j];
+ _children[j + 1] = _children[j];
} else {
break;
}
j--;
}
- _children[j+1] = child;
+ _children[j + 1] = child;
}
this.emit(CHILD_REORDER);
}
@@ -2421,7 +2422,7 @@ var Node = cc.Class({
if (sizeProvider !== this._sgNode) {
sizeProvider.ignoreAnchor = this.__ignoreAnchor;
sizeProvider.setOpacityModifyRGB(this._opacityModifyRGB);
- if ( !this._cascadeOpacityEnabled ) {
+ if (!this._cascadeOpacityEnabled) {
sizeProvider.setOpacity(this._opacity);
}
}
@@ -2656,7 +2657,7 @@ if (CC_JSB) {
*/
var SameNameGetSets = ['parent', 'tag', 'skewX', 'skewY', 'position', 'rotation', 'rotationX', 'rotationY',
- 'scale', 'scaleX', 'scaleY', 'opacity', 'color', ];
+ 'scale', 'scaleX', 'scaleY', 'opacity', 'color',];
var DiffNameGetSets = {
x: ['getPositionX', 'setPositionX'],
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index c76309a2c1..96661e2765 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -5,11 +5,11 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
- worldwide, royalty-free, non-assignable, revocable and non-exclusive license
+ worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
- not use Cocos Creator software for developing other software or tools that's
- used for developing games. You are not granted to publish, distribute,
- sublicense, and/or sell copies of Cocos Creator.
+ not use Cocos Creator software for developing other software or tools that's
+ used for developing games. You are not granted to publish, distribute,
+ sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Chukong Aipu reserves all rights not expressly granted to you.
@@ -38,8 +38,8 @@ var CHILD_REMOVED = 'child-removed';
var idGenerater = new IdGenerater('Node');
-function getConstructor (typeOrClassName) {
- if ( !typeOrClassName ) {
+function getConstructor(typeOrClassName) {
+ if (!typeOrClassName) {
cc.errorID(3804);
return null;
}
@@ -50,7 +50,7 @@ function getConstructor (typeOrClassName) {
return typeOrClassName;
}
-function findComponent (node, constructor) {
+function findComponent(node, constructor) {
for (var i = 0; i < node._components.length; ++i) {
var comp = node._components[i];
if (comp instanceof constructor) {
@@ -60,7 +60,7 @@ function findComponent (node, constructor) {
return null;
}
-function findComponents (node, constructor, components) {
+function findComponents(node, constructor, components) {
for (var i = 0; i < node._components.length; ++i) {
var comp = node._components[i];
if (comp instanceof constructor) {
@@ -69,7 +69,7 @@ function findComponents (node, constructor, components) {
}
}
-function findChildComponent (children, constructor) {
+function findChildComponent(children, constructor) {
for (var i = 0; i < children.length; ++i) {
var node = children[i];
var comp = findComponent(node, constructor);
@@ -86,7 +86,7 @@ function findChildComponent (children, constructor) {
return null;
}
-function findChildComponents (children, constructor, components) {
+function findChildComponents(children, constructor, components) {
for (var i = 0; i < children.length; ++i) {
var node = children[i];
findComponents(node, constructor, components);
@@ -216,7 +216,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
uuid: {
get: function () {
var id = this._id;
- if ( !id ) {
+ if (!id) {
id = this._id = CC_EDITOR ? Editor.Utils.UuidUtils.uuid() : idGenerater.getNewId();
}
return id;
@@ -340,7 +340,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
this._tag = tag;
},
- getParent: function() {
+ getParent: function () {
return this._parent;
},
@@ -444,15 +444,15 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* @example
* var child = node.getChildByUuid(uuid);
*/
- getChildByUuid: function(uuid){
- if(!uuid){
+ getChildByUuid: function (uuid) {
+ if (!uuid) {
cc.log("Invalid uuid");
return null;
}
var locChildren = this._children;
- for(var i = 0, len = locChildren.length; i < len; i++){
- if(locChildren[i]._id === uuid)
+ for (var i = 0, len = locChildren.length; i < len; i++) {
+ if (locChildren[i]._id === uuid)
return locChildren[i];
}
return null;
@@ -467,16 +467,16 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* @example
* var child = node.getChildByName("Test Node");
*/
- getChildByName: function(name){
- if(!name){
+ getChildByName: function (name) {
+ if (!name) {
cc.log("Invalid name");
return null;
}
var locChildren = this._children;
- for(var i = 0, len = locChildren.length; i < len; i++){
- if(locChildren[i]._name === name)
- return locChildren[i];
+ for (var i = 0, len = locChildren.length; i < len; i++) {
+ if (locChildren[i]._name === name)
+ return locChildren[i];
}
return null;
},
@@ -540,7 +540,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
},
- cleanup: function() {
+ cleanup: function () {
},
@@ -796,7 +796,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
var constructor;
if (typeof typeOrClassName === 'string') {
constructor = JS.getClassByName(typeOrClassName);
- if ( !constructor ) {
+ if (!constructor) {
cc.errorID(3807, typeOrClassName);
if (cc._RFpeek()) {
cc.errorID(3808, typeOrClassName);
@@ -805,7 +805,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
}
else {
- if ( !typeOrClassName ) {
+ if (!typeOrClassName) {
cc.errorID(3804);
return null;
}
@@ -874,7 +874,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
if (this._objFlags & Destroying) {
return cc.error('isDestroying');
}
- if ( !(comp instanceof cc.Component) ) {
+ if (!(comp instanceof cc.Component)) {
return cc.errorID(3811);
}
if (index > this._components.length) {
@@ -905,8 +905,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
this._components.splice(index, 0, comp);
if (this._activeInHierarchy) {
- if (typeof comp.__preload === 'function' &&
- !(comp._objFlags & cc.Object.Flags.IsPreloadCalled)) {
+ if (typeof comp.__preload === 'function' && !(comp._objFlags & cc.Object.Flags.IsPreloadCalled)) {
cc.Component._callPreloadOnComponent(comp);
}
// call onLoad/onEnable
@@ -930,7 +929,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* node.removeComponent(Test);
*/
removeComponent: function (component) {
- if ( !component ) {
+ if (!component) {
cc.errorID(3813);
return;
}
@@ -1198,7 +1197,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
// detach self and children from editor
var parent = this._parent;
var destroyByParent = parent && (parent._objFlags & Destroying);
- if ( !destroyByParent ) {
+ if (!destroyByParent) {
if (CC_EDITOR || CC_TEST) {
this._registerIfAttached(false);
}
@@ -1230,7 +1229,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
cc.game.removePersistRootNode(this);
}
- if ( !destroyByParent ) {
+ if (!destroyByParent) {
// remove from parent
if (parent) {
var childIndex = parent._children.indexOf(this);
From caa0938b684e3dc2c9d2e70035045ccb5b84eceb Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 10:17:34 +0800
Subject: [PATCH 29/36] adjust the order of init
---
cocos2d/core/CCNode.js | 27 +++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index e9c511819a..87d2f6b6dd 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -840,20 +840,6 @@ var Node = cc.Class({
ctor: function (name) {
- // cache component
- this._widget = null;
-
- // Touch event listener
- this._touchListener = null;
-
- // Mouse event listener
- this._mouseListener = null;
-
- // Retained actions for JSB
- if (CC_JSB) {
- this._retainedActions = [];
- }
-
/**
* Current scene graph node for this node.
*
@@ -890,6 +876,19 @@ var Node = cc.Class({
this.__ignoreAnchor = false;
this._reorderChildDirty = false;
+ // cache component
+ this._widget = null;
+
+ // Touch event listener
+ this._touchListener = null;
+
+ // Mouse event listener
+ this._mouseListener = null;
+
+ // Retained actions for JSB
+ if (CC_JSB) {
+ this._retainedActions = [];
+ }
},
//statics: {
From b2645dabaca58a988b444e08e72575867e4e4ae5 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 10:47:18 +0800
Subject: [PATCH 30/36] refactor onRestore function
---
cocos2d/core/CCNode.js | 7 +------
cocos2d/core/utils/base-node.js | 1 +
2 files changed, 2 insertions(+), 6 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 87d2f6b6dd..bcc59f8678 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2438,12 +2438,7 @@ var Node = cc.Class({
}
}
- // check activity state
- var shouldActiveInHierarchy = (this._parent && this._parent._activeInHierarchy && this._active);
- if (shouldActiveInHierarchy !== this._activeInHierarchy) {
- this._onActivatedInHierarchy(shouldActiveInHierarchy);
- this.emit('active-in-hierarchy-changed', this);
- }
+ this._onRestoreBase();
if (this._activeInHierarchy) {
cc.director.getActionManager().resumeTarget(this);
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 96661e2765..f5a1b3b541 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -1268,4 +1268,5 @@ var DiffNameGetSets = {
};
Misc.propertyDefine(BaseNode, SameNameGetSets, DiffNameGetSets);
BaseNode.prototype._onHierarchyChangedBase = BaseNode.prototype._onHierarchyChanged;
+BaseNode.prototype._onRestoreBase = BaseNode.prototype.onRestore;
cc._BaseNode = module.exports = BaseNode;
From d087040b890d9f64033fba82f0fcb0d8b4c0b5d3 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 10:47:40 +0800
Subject: [PATCH 31/36] update comments/docs, remove used codes
---
cocos2d/core/CCNode.js | 9 ---------
cocos2d/core/utils/base-node.js | 32 ++++++++++++++++----------------
2 files changed, 16 insertions(+), 25 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index bcc59f8678..062f361413 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -2521,15 +2521,6 @@ if (CC_JSB) {
* @event group-changed
* @param {Event} event
*/
-/**
- * !#en
- * Note: This event is only emitted from the top most node whose active value did changed,
- * not including its child nodes.
- * !#zh
- * 注意:此节点激活时,此事件仅从最顶部的节点发出。
- * @event active-in-hierarchy-changed
- * @param {Event} event
- */
/**
*
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index f5a1b3b541..12f884b910 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -97,14 +97,12 @@ function findChildComponents(children, constructor, components) {
}
/**
- * A base node for CCNode and CCEScene, it will:
- * - provide the same api with origin cocos2d rendering node (SGNode)
- * - maintains properties of the internal SGNode
- * - retain and release the SGNode
- * - serialize datas for SGNode (but SGNode itself will not being serialized)
+ * A base node for CCNode, it will:
+ * - maintain scene hierarchy and active logic
* - notifications if some properties changed
- * - define some interfaces shares between CCNode and CCEScene
- *
+ * - define some interfaces shares between CCNode
+ * - define machanisms for Enity Component Systems
+ * - define prefab and serialize functions
*
* @class _BaseNode
* @extends Object
@@ -1043,12 +1041,12 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
}
}
}
+
this._objFlags &= ~Activating;
if (!cancelActivation && this._onActive_EventsActions) {
this._onActive_EventsActions(newActive);
}
-
},
_deactivateChildComponents: function () {
@@ -1255,18 +1253,20 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
},
});
+/**
+ * !#en
+ * Note: This event is only emitted from the top most node whose active value did changed,
+ * not including its child nodes.
+ * !#zh
+ * 注意:此节点激活时,此事件仅从最顶部的节点发出。
+ * @event active-in-hierarchy-changed
+ * @param {Event} event
+ */
// Define public getter and setter methods to ensure api compatibility.
var SameNameGetSets = ['name', 'children', 'childrenCount',];
-var DiffNameGetSets = {
- //// privates
- //width: ['_getWidth', '_setWidth'],
- //height: ['_getHeight', '_setHeight'],
- //anchorX: ['_getAnchorX', '_setAnchorX'],
- //anchorY: ['_getAnchorY', '_setAnchorY'],
-};
-Misc.propertyDefine(BaseNode, SameNameGetSets, DiffNameGetSets);
+Misc.propertyDefine(BaseNode, SameNameGetSets, {});
BaseNode.prototype._onHierarchyChangedBase = BaseNode.prototype._onHierarchyChanged;
BaseNode.prototype._onRestoreBase = BaseNode.prototype.onRestore;
cc._BaseNode = module.exports = BaseNode;
From 30c2f8947ba951bdb56856a5706bbe2ee7fca929 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 11:26:16 +0800
Subject: [PATCH 32/36] refactor setSiblingIndex
---
cocos2d/core/CCNode.js | 79 +++++++++++++--------------------
cocos2d/core/utils/base-node.js | 15 ++++---
2 files changed, 39 insertions(+), 55 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 062f361413..d4033aa81e 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -1458,58 +1458,41 @@ var Node = cc.Class({
}
},
- setSiblingIndex: function (index) {
- if (!this._parent) {
- return;
- }
- var array = this._parent._children;
- index = index !== -1 ? index : array.length - 1;
- var oldIndex = array.indexOf(this);
- if (index !== oldIndex) {
- array.splice(oldIndex, 1);
- if (index < array.length) {
- array.splice(index, 0, this);
- }
- else {
- array.push(this);
- }
-
- // update rendering scene graph, sort them by arrivalOrder
- var parent = this._parent;
- var siblings = parent._children;
-
- var i = 0, len = siblings.length, sibling;
- if (CC_JSB) {
- if (cc.runtime) {
- for (; i < len; i++) {
- sibling = siblings[i]._sgNode;
- // Reset zorder to update their arrival order
- var zOrder = sibling.getLocalZOrder();
- sibling.setLocalZOrder(zOrder + 1);
- sibling.setLocalZOrder(zOrder);
- }
- }
- else {
- parent._sgNode.removeChild(this._sgNode, false);
- if (index + 1 < array.length) {
- var nextSibling = array[index + 1];
- parent._sgNode.insertChildBefore(this._sgNode, nextSibling._sgNode);
- }
- else {
- parent._sgNode.addChild(this._sgNode);
- }
- }
- }
- else {
+ _onSiblingIndexChanged: function (index) {
+ // update rendering scene graph, sort them by arrivalOrder
+ var parent = this._parent;
+ var siblings = parent._children;
+ var i = 0, len = siblings.length, sibling;
+ if (CC_JSB) {
+ if (cc.runtime) {
for (; i < len; i++) {
sibling = siblings[i]._sgNode;
- sibling._arrivalOrder = i;
- cc.eventManager._setDirtyForNode(sibling);
+ // Reset zorder to update their arrival order
+ var zOrder = sibling.getLocalZOrder();
+ sibling.setLocalZOrder(zOrder + 1);
+ sibling.setLocalZOrder(zOrder);
}
- cc.renderer.childrenOrderDirty = true;
- parent._sgNode._reorderChildDirty = true;
- parent._delaySort();
}
+ else {
+ parent._sgNode.removeChild(this._sgNode, false);
+ if (index + 1 < siblings.length) {
+ var nextSibling = siblings[index + 1];
+ parent._sgNode.insertChildBefore(this._sgNode, nextSibling._sgNode);
+ }
+ else {
+ parent._sgNode.addChild(this._sgNode);
+ }
+ }
+ }
+ else {
+ for (; i < len; i++) {
+ sibling = siblings[i]._sgNode;
+ sibling._arrivalOrder = i;
+ cc.eventManager._setDirtyForNode(sibling);
+ }
+ cc.renderer.childrenOrderDirty = true;
+ parent._sgNode._reorderChildDirty = true;
+ parent._delaySort();
}
},
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 12f884b910..88868691a6 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -524,17 +524,18 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
if (!this._parent) {
return;
}
- var array = this._parent._children;
- index = index !== -1 ? index : array.length - 1;
- var oldIndex = array.indexOf(this);
+ var siblings = this._parent._children;
+ index = index !== -1 ? index : siblings.length - 1;
+ var oldIndex = siblings.indexOf(this);
if (index !== oldIndex) {
- array.splice(oldIndex, 1);
- if (index < array.length) {
- array.splice(index, 0, this);
+ siblings.splice(oldIndex, 1);
+ if (index < siblings.length) {
+ siblings.splice(index, 0, this);
}
else {
- array.push(this);
+ siblings.push(this);
}
+ this._onSiblingIndexChanged && this._onSiblingIndexChanged(index);
}
},
From 3d3268dc57a2fa23c4b95e251c288b779425d6a4 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 11:38:17 +0800
Subject: [PATCH 33/36] extract _updateOrder
---
cocos2d/core/CCNode.js | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index d4033aa81e..1abc87f858 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -826,10 +826,7 @@ var Node = cc.Class({
this._sgNode.zIndex = value;
if (this._parent) {
- this._parent._delaySort();
- if (!CC_JSB) {
- cc.eventManager._setDirtyForNode(this);
- }
+ this._updateOrder();
}
}
}
@@ -1435,10 +1432,7 @@ var Node = cc.Class({
if (value) {
var parent = value._sgNode;
parent.addChild(sgNode);
- value._delaySort();
- if (!CC_JSB) {
- cc.eventManager._setDirtyForNode(this);
- }
+ this._updateOrder();
value._children.push(this);
value.emit(CHILD_ADDED, this);
}
@@ -1458,6 +1452,13 @@ var Node = cc.Class({
}
},
+ _updateOrder: function() {
+ this._parent && this._parent._delaySort();
+ if (!CC_JSB) {
+ cc.eventManager._setDirtyForNode(this);
+ }
+ },
+
_onSiblingIndexChanged: function (index) {
// update rendering scene graph, sort them by arrivalOrder
var parent = this._parent;
From f97896c4838acb5b8b0a920b6e85a8f0b7521204 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 11:45:45 +0800
Subject: [PATCH 34/36] scene cannot be _instantiate
---
cocos2d/core/CCScene.js | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/cocos2d/core/CCScene.js b/cocos2d/core/CCScene.js
index c551496327..2243bc7e07 100644
--- a/cocos2d/core/CCScene.js
+++ b/cocos2d/core/CCScene.js
@@ -84,7 +84,8 @@ cc.Scene = cc.Class({
},
_onHierarchyChanged: NIL,
-
+ _instantiate : null,
+
_load: function () {
if ( ! this._inited) {
From 2f18254789b94a15646a7395ac025c103234a786 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 11:57:55 +0800
Subject: [PATCH 35/36] reformat codes
---
cocos2d/core/CCNode.js | 6 +++---
cocos2d/core/CCScene.js | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 1abc87f858..fc44f16f27 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -1013,7 +1013,7 @@ var Node = cc.Class({
},
_onActive_EventsActions: function (newActive) {
-// ActionManager, EventManager
+ // ActionManager, EventManager
if (newActive) {
// activate
cc.director.getActionManager().resumeTarget(this);
@@ -1452,7 +1452,7 @@ var Node = cc.Class({
}
},
- _updateOrder: function() {
+ _updateOrder: function () {
this._parent && this._parent._delaySort();
if (!CC_JSB) {
cc.eventManager._setDirtyForNode(this);
@@ -1460,7 +1460,7 @@ var Node = cc.Class({
},
_onSiblingIndexChanged: function (index) {
- // update rendering scene graph, sort them by arrivalOrder
+ // update rendering scene graph, sort them by arrivalOrder
var parent = this._parent;
var siblings = parent._children;
var i = 0, len = siblings.length, sibling;
diff --git a/cocos2d/core/CCScene.js b/cocos2d/core/CCScene.js
index 2243bc7e07..cd77fd3cdf 100644
--- a/cocos2d/core/CCScene.js
+++ b/cocos2d/core/CCScene.js
@@ -85,7 +85,7 @@ cc.Scene = cc.Class({
_onHierarchyChanged: NIL,
_instantiate : null,
-
+
_load: function () {
if ( ! this._inited) {
From 8416ed27f7def3055cbe17bc5762b4036aa93ff2 Mon Sep 17 00:00:00 2001
From: "Huabing.Xu"
Date: Thu, 29 Dec 2016 14:36:07 +0800
Subject: [PATCH 36/36] small adjust of code
---
cocos2d/core/CCNode.js | 18 +++++++++---------
cocos2d/core/utils/base-node.js | 7 +++++--
2 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index fc44f16f27..57b395758f 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -263,6 +263,13 @@ var _searchMaskParent = function (node) {
return null;
};
+function updateOrder (node) {
+ node._parent._delaySort();
+ if (!CC_JSB) {
+ cc.eventManager._setDirtyForNode(node);
+ }
+};
+
/**
* !#en
* Class of all entities in Cocos Creator scenes.
@@ -826,7 +833,7 @@ var Node = cc.Class({
this._sgNode.zIndex = value;
if (this._parent) {
- this._updateOrder();
+ updateOrder(this);
}
}
}
@@ -1432,7 +1439,7 @@ var Node = cc.Class({
if (value) {
var parent = value._sgNode;
parent.addChild(sgNode);
- this._updateOrder();
+ updateOrder(this);
value._children.push(this);
value.emit(CHILD_ADDED, this);
}
@@ -1452,13 +1459,6 @@ var Node = cc.Class({
}
},
- _updateOrder: function () {
- this._parent && this._parent._delaySort();
- if (!CC_JSB) {
- cc.eventManager._setDirtyForNode(this);
- }
- },
-
_onSiblingIndexChanged: function (index) {
// update rendering scene graph, sort them by arrivalOrder
var parent = this._parent;
diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js
index 88868691a6..5dc143697a 100644
--- a/cocos2d/core/utils/base-node.js
+++ b/cocos2d/core/utils/base-node.js
@@ -904,7 +904,8 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
this._components.splice(index, 0, comp);
if (this._activeInHierarchy) {
- if (typeof comp.__preload === 'function' && !(comp._objFlags & cc.Object.Flags.IsPreloadCalled)) {
+ if (typeof comp.__preload === 'function' &&
+ !(comp._objFlags & cc.Object.Flags.IsPreloadCalled)) {
cc.Component._callPreloadOnComponent(comp);
}
// call onLoad/onEnable
@@ -1269,5 +1270,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
var SameNameGetSets = ['name', 'children', 'childrenCount',];
Misc.propertyDefine(BaseNode, SameNameGetSets, {});
BaseNode.prototype._onHierarchyChangedBase = BaseNode.prototype._onHierarchyChanged;
-BaseNode.prototype._onRestoreBase = BaseNode.prototype.onRestore;
+if(CC_EDITOR) {
+ BaseNode.prototype._onRestoreBase = BaseNode.prototype.onRestore;
+}
cc._BaseNode = module.exports = BaseNode;