diff --git a/src/core/display/Container.js b/src/core/display/Container.js index 34931bb..03b8464 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -103,13 +103,43 @@ /** * Adds a child to the container. - * + * + * You can also add multple items like so: myContainer.addChild(thinkOne, thingTwo, thingThree) * @param child {PIXI.DisplayObject} The DisplayObject to add to the container * @return {PIXI.DisplayObject} The child that was added. */ Container.prototype.addChild = function (child) -{ - return this.addChildAt(child, this.children.length); +{ + var argumentsLength = arguments.length; + + // if there is only one argument we can bypass looping through the them + if(argumentsLength > 1) + { + // loop through the arguments property and add all children + // use it the right way (.length and [i]) so that this function can still be optimised by JS runtimes + for (var i = 0; i < argumentsLength; i++) + { + this.addChild( arguments[i] ); + } + } + else + { + // if the child has a parent then lets remove it as Pixi objects can only exist in one place + if (child.parent) + { + child.parent.removeChild(child); + } + + child.parent = this; + + this.children.push(child); + + // TODO - lets either do all callbacks or all events.. not both! + this.onChildrenChange(this.children.length-1); + child.emit('added', this); + }; + + return child; }; /** @@ -121,12 +151,6 @@ */ Container.prototype.addChildAt = function (child, index) { - // prevent adding self as child - if (child === this) - { - return child; - } - if (index >= 0 && index <= this.children.length) { if (child.parent) @@ -137,8 +161,9 @@ child.parent = this; this.children.splice(index, 0, child); - this.onChildrenChange(index); + // TODO - lets either do all callbacks or all events.. not both! + this.onChildrenChange(index); child.emit('added', this); return child; @@ -237,14 +262,36 @@ */ Container.prototype.removeChild = function (child) { - var index = this.children.indexOf(child); + var argumentsLength = arguments.length; - if (index === -1) + // if there is only one argument we can bypass looping through the them + if(argumentsLength > 1) { - return; + // loop through the arguments property and add all children + // use it the right way (.length and [i]) so that this function can still be optimised by JS runtimes + for (var i = 0; i < argumentsLength; i++) + { + this.removeChild( arguments[i] ); + } + } + else + { + var index = this.children.indexOf(child); + + if (index === -1) + { + return; + } + + child.parent = null; + this.children.splice(index, 1); + + // TODO - lets either do all callbacks or all events.. not both! + this.onChildrenChange(index); + child.emit('removed', this); } - return this.removeChildAt(index); + return child }; /** @@ -259,8 +306,9 @@ child.parent = null; this.children.splice(index, 1); - this.onChildrenChange(index); + // TODO - lets either do all callbacks or all events.. not both! + this.onChildrenChange(index); child.emit('removed', this); return child;