/** * A Smart version of DisplayObjectContainer's builtin function, ie: target.getBounds(targetCoordinateSpace) * When objects are nested, this function will produce a tighter result than DisplayObjectContainer.getBounds() * Note: This function works on any DisplayObject, even if there are no nested children * Note: Empty child DisplayObjects are essentially ignored * param: target: see Sprite.getBounds() * param: targetCoordinateSpace: see Sprite.getBounds() * return: Rectangle bounds */ public static function getNestedBounds(target:DisplayObject, targetCoordinateSpace:DisplayObject):Rectangle { if (target.hasOwnProperty("numChildren") && (target as DisplayObjectContainer).numChildren > 0) { var j:int = 1; var nbounds:Rectangle = getNestedBounds((target as DisplayObjectContainer).getChildAt(0), targetCoordinateSpace); while ((nbounds.width == 0 || nbounds.height == 0) && j < (target as DisplayObjectContainer).numChildren) { nbounds = getNestedBounds((target as DisplayObjectContainer).getChildAt(j), targetCoordinateSpace); j++; } for (var i:int = j; i < (target as DisplayObjectContainer).numChildren; i++ ) { var bounds:Rectangle = getNestedBounds((target as DisplayObjectContainer).getChildAt(i), targetCoordinateSpace); if (bounds.width > 0 && bounds.height > 0) { var x0:Number = Math.min(bounds.x, nbounds.x); var y0:Number = Math.min(bounds.y, nbounds.y); var x1:Number = Math.max(bounds.x + bounds.width, nbounds.x + nbounds.width); var y1:Number = Math.max(bounds.y + bounds.height, nbounds.y + nbounds.height); nbounds.x = x0; nbounds.y = y0; nbounds.width = x1 - x0; nbounds.height = y1 - y0; } } return nbounds; } else { return target.getBounds(targetCoordinateSpace); } }