Skip to content

Commit f2a8a92

Browse files
committed
Group.addMultiple allows you to pass an array of game objects and they'll all be added to the Group in turn.
1 parent cb14fd1 commit f2a8a92

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Version 2.1.0 - "Cairhien" - -in development-
108108
* Phaser.Canvas.setImageRenderingCrisp now sets `image-rendering: pixelated`, perfect for pixel art, which is now supported in Chrome 38.
109109
* Phaser.Mouse will now add a listener to the `window` to detect `mouseup` events. This is used to detect if the player releases the mouse while outside of the game canvas. Previously Pointer objects incorrectly thought they were still pressed when you returned the mouse over the canvas (#1167)
110110
* Rectangle.centerOn(x,y) allows you to quickly center a Rectangle on the given coordinates.
111+
* Group.addMultiple allows you to pass an array of game objects and they'll all be added to the Group in turn.
111112

112113
### Updates
113114

src/core/Group.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Phaser.Group.SORT_DESCENDING = 1;
194194
* @see Phaser.Group#create
195195
* @see Phaser.Group#addAt
196196
* @method Phaser.Group#add
197-
* @param {*} child - An instance of Phaser.Sprite, Phaser.Button or any other display object..
197+
* @param {*} child - An instance of Phaser.Sprite, Phaser.Button or any other display object.
198198
* @param {boolean} [silent=false] - If the silent parameter is `true` the child will not dispatch the onAddedToGroup event.
199199
* @return {*} The child that was added to the Group.
200200
*/
@@ -228,6 +228,30 @@ Phaser.Group.prototype.add = function (child, silent) {
228228

229229
};
230230

231+
/**
232+
* Adds an array existing objects to this Group. The objects can be instances of Phaser.Sprite, Phaser.Button or any other display object.
233+
* The children are automatically added to the top of the Group, so render on-top of everything else within the Group.
234+
* TODO: Add ability to pass the children as parameters rather than having to be an array.
235+
*
236+
* @method Phaser.Group#addMultiple
237+
* @param {array} children - An array containing instances of Phaser.Sprite, Phaser.Button or any other display object.
238+
* @param {boolean} [silent=false] - If the silent parameter is `true` the children will not dispatch the onAddedToGroup event.
239+
* @return {*} The array of children that were added to the Group.
240+
*/
241+
Phaser.Group.prototype.addMultiple = function (children, silent) {
242+
243+
if (Array.isArray(children))
244+
{
245+
for (var i = 0; i < children.length; i++)
246+
{
247+
this.add(children[i], silent);
248+
}
249+
}
250+
251+
return children;
252+
253+
};
254+
231255
/**
232256
* Adds an existing object to this Group. The object can be an instance of Phaser.Sprite, Phaser.Button or any other display object.
233257
* The child is added to the Group at the location specified by the index value, this allows you to control child ordering.

0 commit comments

Comments
 (0)