diff --git a/src/core/display/Container.js b/src/core/display/Container.js index 9410af2..0352095 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -563,7 +563,8 @@ }; /** - * Destroys the container + * Removes all internal references and listeners as well as removes children from the display list. + * Do not use a Container after calling `destroy`. * @param [options] {object|boolean} Options parameter. A boolean will act as if all options have been set to that value * @param [options.children=false] {boolean} if set to true, all the children will have their destroy * method called as well. 'options' will be passed on to those calls. @@ -573,15 +574,17 @@ DisplayObject.prototype.destroy.call(this); var destroyChildren = typeof options === 'boolean' ? options : options && options.children; + + var oldChildren = this.children; + this.children = null; + if (destroyChildren) { - for (var i = 0, j = this.children.length; i < j; ++i) + for (var i = oldChildren.length - 1; i >= 0; i--) { - this.children[i].destroy(options); + var child = oldChildren[i]; + child.parent = null; + child.destroy(options); } } - - this.removeChildren(); - - this.children = null; }; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index 9410af2..0352095 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -563,7 +563,8 @@ }; /** - * Destroys the container + * Removes all internal references and listeners as well as removes children from the display list. + * Do not use a Container after calling `destroy`. * @param [options] {object|boolean} Options parameter. A boolean will act as if all options have been set to that value * @param [options.children=false] {boolean} if set to true, all the children will have their destroy * method called as well. 'options' will be passed on to those calls. @@ -573,15 +574,17 @@ DisplayObject.prototype.destroy.call(this); var destroyChildren = typeof options === 'boolean' ? options : options && options.children; + + var oldChildren = this.children; + this.children = null; + if (destroyChildren) { - for (var i = 0, j = this.children.length; i < j; ++i) + for (var i = oldChildren.length - 1; i >= 0; i--) { - this.children[i].destroy(options); + var child = oldChildren[i]; + child.parent = null; + child.destroy(options); } } - - this.removeChildren(); - - this.children = null; }; diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 83dc97f..7716d33 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -579,11 +579,18 @@ }; /** - * Base destroy method for generic display objects - * + * Base destroy method for generic display objects. This will automatically + * remove the display object from its parent Container as well as remove + * all current event listeners and internal references. Do not use a DisplayObject + * after calling `destroy`. */ DisplayObject.prototype.destroy = function () { + this.removeAllListeners(); + if (this.parent) + { + this.parent.removeChild(this); + } this.transform = null; this.parent = null;