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] 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; }, /**