From a2c4ed7891bfddecf86a12bbcbcd15170213ddcf Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Thu, 25 Feb 2016 13:47:48 +0800 Subject: [PATCH 1/2] fixed getBoundingBoxToWorld --- cocos2d/core/utils/base-node.js | 51 +++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 006bf8fcad..b0699d9757 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -1258,12 +1258,59 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, /** - * Returns a "world" axis aligned bounding box of the node. + * Returns a "world" axis aligned bounding box of the node.
+ * The bounding box contains self and active children's world bounding box. * @method getBoundingBoxToWorld * @return {Rect} */ getBoundingBoxToWorld: function () { - return this._sgNode.getBoundingBoxToWorld(); + var size = this.getContentSize(); + var width = size.width; + var height = size.height; + var rect = cc.rect(-this.anchorX * width, -this.anchorY * height, width, height); + + var trans = this.getNodeToWorldTransform(); + cc._rectApplyAffineTransformIn(rect, trans); + + //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(trans); + if (childRect) + rect = cc.rectUnion(rect, childRect); + } + } + return rect; + }, + + _getBoundingBoxTo: function (parentTransform) { + var size = this.getContentSize(); + var width = size.width; + var height = size.height; + var rect = cc.rect(-this.anchorX * width, -this.anchorY * height, width, height); + + var trans = (parentTransform === undefined) ? this.getNodeToParentTransform() : cc.affineTransformConcat(this.getNodeToParentTransform(), parentTransform); + cc._rectApplyAffineTransformIn(rect, trans); + + //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(trans); + if (childRect) + rect = cc.rectUnion(rect, childRect); + } + } + return rect; }, /** From 2afb3f47ea0978097f89e8e5a4326a817a6ec240 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Thu, 25 Feb 2016 14:35:29 +0800 Subject: [PATCH 2/2] optimize code --- cocos2d/core/utils/base-node.js | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index b0699d9757..119e2276f7 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -1264,28 +1264,11 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ * @return {Rect} */ getBoundingBoxToWorld: function () { - var size = this.getContentSize(); - var width = size.width; - var height = size.height; - var rect = cc.rect(-this.anchorX * width, -this.anchorY * height, width, height); - - var trans = this.getNodeToWorldTransform(); - cc._rectApplyAffineTransformIn(rect, trans); - - //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(trans); - if (childRect) - rect = cc.rectUnion(rect, childRect); - } + var trans; + if (this.parent) { + trans = this.parent.getNodeToWorldTransform(); } - return rect; + return this._getBoundingBoxTo(trans); }, _getBoundingBoxTo: function (parentTransform) {