Skip to content

Commit 24f4abe

Browse files
committed
Group.add has a new optional argument index which controls the index within the group to insert the child to. Where 0 is the bottom of the Group.
Group.addAt has been refactored to be a simple call to `Group.add`, removing lots of duplicate code in the process. Group.create has a new optional argument `index` which controls the index within the group to insert the child to. Where 0 is the bottom of the Group. It also now makes proper use of `Group.add`, cutting down on more duplicate code. Group.createMultiple now returns an Array containing references to all of the children that the method created.
1 parent c64749f commit 24f4abe

1 file changed

Lines changed: 120 additions & 102 deletions

File tree

src/core/Group.js

Lines changed: 120 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,22 @@ Phaser.Group = function (game, parent, name, addToStage, enableBody, physicsBody
137137
*/
138138
this.cursor = null;
139139

140+
/**
141+
* A Group with `inputEnableChildren` set to `true` will automatically call `inputEnabled = true`
142+
* on any children _added_ to, or _created by_, this Group.
143+
*
144+
* If there are children already in the Group at the time you set this property, they are not changed.
145+
*
146+
* @property {boolean} inputEnableChildren
147+
* @default
148+
*/
149+
this.inputEnableChildren = false;
150+
140151
/**
141152
* If true all Sprites created by, or added to this group, will have a physics body enabled on them.
142153
*
154+
* If there are children already in the Group at the time you set this property, they are not changed.
155+
*
143156
* The default body type is controlled with {@link #physicsBodyType}.
144157
* @property {boolean} enableBody
145158
*/
@@ -272,56 +285,101 @@ Phaser.Group.SORT_DESCENDING = 1;
272285
/**
273286
* Adds an existing object as the top child in this group.
274287
*
275-
* The child is automatically added to the top of the group and is displayed on top of every previous child.
288+
* The child is automatically added to the top of the group, and is displayed above every previous child.
289+
*
290+
* Or if the _optional_ index is specified, the child is added at the location specified by the index value,
291+
* this allows you to control child ordering.
292+
*
293+
* If the child was already in this Group, it is simply returned, and nothing else happens to it.
276294
*
277-
* If Group.enableBody is set then a physics body will be created on the object, so long as one does not already exist.
295+
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
296+
*
297+
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
278298
*
279299
* Use {@link #addAt} to control where a child is added. Use {@link #create} to create and add a new child.
280300
*
281301
* @method Phaser.Group#add
282302
* @param {DisplayObject} child - The display object to add as a child.
283303
* @param {boolean} [silent=false] - If true the child will not dispatch the `onAddedToGroup` event.
304+
* @param {integer} [index] - The index within the group to insert the child to. Where 0 is the bottom of the Group.
284305
* @return {DisplayObject} The child that was added to the group.
285306
*/
286-
Phaser.Group.prototype.add = function (child, silent) {
307+
Phaser.Group.prototype.add = function (child, silent, index) {
287308

288309
if (silent === undefined) { silent = false; }
289310

290-
if (child.parent !== this)
311+
if (child.parent === this)
291312
{
292-
if (child.body && child.parent && child.parent.hash)
293-
{
294-
child.parent.removeFromHash(child);
295-
}
313+
return child;
314+
}
315+
316+
if (child.body && child.parent && child.parent.hash)
317+
{
318+
child.parent.removeFromHash(child);
319+
}
296320

321+
if (index === undefined)
322+
{
297323
child.z = this.children.length;
298324

299325
this.addChild(child);
326+
}
327+
else
328+
{
329+
this.addChildAt(child, index);
300330

301-
if (this.enableBody && child.body === null)
302-
{
303-
this.game.physics.enable(child, this.physicsBodyType);
304-
}
305-
else if (child.body)
306-
{
307-
this.addToHash(child);
308-
}
331+
this.updateZ();
332+
}
309333

310-
if (!silent && child.events)
311-
{
312-
child.events.onAddedToGroup$dispatch(child, this);
313-
}
334+
if (this.enableBody && child.hasProperty('body') && child.body === null)
335+
{
336+
this.game.physics.enable(child, this.physicsBodyType);
337+
}
338+
else if (child.body)
339+
{
340+
this.addToHash(child);
341+
}
314342

315-
if (this.cursor === null)
316-
{
317-
this.cursor = child;
318-
}
343+
if (this.inputEnableChildren && child.hasProperty('input') && !child.inputEnabled)
344+
{
345+
child.inputEnabled = true;
346+
}
347+
348+
if (!silent && child.events)
349+
{
350+
child.events.onAddedToGroup$dispatch(child, this);
351+
}
352+
353+
if (this.cursor === null)
354+
{
355+
this.cursor = child;
319356
}
320357

321358
return child;
322359

323360
};
324361

362+
/**
363+
* Adds an existing object to this group.
364+
*
365+
* The child is added to the group at the location specified by the index value, this allows you to control child ordering.
366+
*
367+
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
368+
*
369+
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
370+
*
371+
* @method Phaser.Group#addAt
372+
* @param {DisplayObject} child - The display object to add as a child.
373+
* @param {integer} [index=0] - The index within the group to insert the child to.
374+
* @param {boolean} [silent=false] - If true the child will not dispatch the `onAddedToGroup` event.
375+
* @return {DisplayObject} The child that was added to the group.
376+
*/
377+
Phaser.Group.prototype.addAt = function (child, index, silent) {
378+
379+
this.add(child, silent, index);
380+
381+
};
382+
325383
/**
326384
* Adds a child of this Group into the hash array.
327385
* This call will return false if the child is not a child of this Group, or is already in the hash.
@@ -379,6 +437,10 @@ Phaser.Group.prototype.removeFromHash = function (child) {
379437
*
380438
* As well as an array you can also pass another Group as the first argument. In this case all of the children from that
381439
* Group will be removed from it and added into this Group.
440+
*
441+
* If `Group.enableBody` is set, then a physics body will be created on the objects, so long as one does not already exist.
442+
*
443+
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the objects, so long as one does not already exist.
382444
*
383445
* @method Phaser.Group#addMultiple
384446
* @param {DisplayObject[]|Phaser.Group} children - An array of display objects or a Phaser.Group. If a Group is given then *all* children will be moved from it.
@@ -403,56 +465,6 @@ Phaser.Group.prototype.addMultiple = function (children, silent) {
403465

404466
};
405467

406-
/**
407-
* Adds an existing object to this group.
408-
*
409-
* The child is added to the group at the location specified by the index value, this allows you to control child ordering.
410-
*
411-
* @method Phaser.Group#addAt
412-
* @param {DisplayObject} child - The display object to add as a child.
413-
* @param {integer} [index=0] - The index within the group to insert the child to.
414-
* @param {boolean} [silent=false] - If true the child will not dispatch the `onAddedToGroup` event.
415-
* @return {DisplayObject} The child that was added to the group.
416-
*/
417-
Phaser.Group.prototype.addAt = function (child, index, silent) {
418-
419-
if (silent === undefined) { silent = false; }
420-
421-
if (child.parent !== this)
422-
{
423-
if (child.body && child.parent)
424-
{
425-
child.parent.removeFromHash(child);
426-
}
427-
428-
this.addChildAt(child, index);
429-
430-
this.updateZ();
431-
432-
if (this.enableBody && child.body === null)
433-
{
434-
this.game.physics.enable(child, this.physicsBodyType);
435-
}
436-
else if (child.body)
437-
{
438-
this.addToHash(child);
439-
}
440-
441-
if (!silent && child.events)
442-
{
443-
child.events.onAddedToGroup$dispatch(child, this);
444-
}
445-
446-
if (this.cursor === null)
447-
{
448-
this.cursor = child;
449-
}
450-
}
451-
452-
return child;
453-
454-
};
455-
456468
/**
457469
* Returns the child found at the given index within this group.
458470
*
@@ -477,16 +489,26 @@ Phaser.Group.prototype.getAt = function (index) {
477489
* Creates a new Phaser.Sprite object and adds it to the top of this group.
478490
*
479491
* Use {@link #classType} to change the type of object created.
492+
*
493+
* The child is automatically added to the top of the group, and is displayed above every previous child.
494+
*
495+
* Or if the _optional_ index is specified, the child is added at the location specified by the index value,
496+
* this allows you to control child ordering.
497+
*
498+
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
499+
*
500+
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
480501
*
481502
* @method Phaser.Group#create
482503
* @param {number} x - The x coordinate to display the newly created Sprite at. The value is in relation to the group.x point.
483504
* @param {number} y - The y coordinate to display the newly created Sprite at. The value is in relation to the group.y point.
484505
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|Phaser.Video|PIXI.Texture} [key] - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache Image entry, or an instance of a RenderTexture, BitmapData, Video or PIXI.Texture.
485506
* @param {string|number} [frame] - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
486507
* @param {boolean} [exists=true] - The default exists state of the Sprite.
508+
* @param {integer} [index] - The index within the group to insert the child to. Where 0 is the bottom of the Group.
487509
* @return {DisplayObject} The child that was created: will be a {@link Phaser.Sprite} unless {@link #classType} has been changed.
488510
*/
489-
Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
511+
Phaser.Group.prototype.create = function (x, y, key, frame, exists, index) {
490512

491513
if (exists === undefined) { exists = true; }
492514

@@ -496,52 +518,44 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
496518
child.visible = exists;
497519
child.alive = exists;
498520

499-
child.z = this.children.length;
500-
501-
this.addChild(child);
502-
503-
if (this.enableBody)
504-
{
505-
this.game.physics.enable(child, this.physicsBodyType, this.enableBodyDebug);
506-
}
507-
508-
if (child.events)
509-
{
510-
child.events.onAddedToGroup$dispatch(child, this);
511-
}
512-
513-
if (this.cursor === null)
514-
{
515-
this.cursor = child;
516-
}
517-
518-
return child;
521+
return this.add(child, false, index);
519522

520523
};
521524

522525
/**
523-
* Creates multiple Phaser.Sprite objects and adds them to the top of this group.
526+
* Creates multiple Phaser.Sprite objects and adds them to the top of this Group.
524527
*
525-
* Useful if you need to quickly generate a pool of identical sprites, such as bullets.
526-
*
527-
* By default the sprites will be set to not exist and will be positioned at 0, 0 (relative to the group.x/y).
528528
* Use {@link #classType} to change the type of object created.
529529
*
530+
* This method is useful if you need to quickly generate a pool of identical sprites, such as bullets.
531+
*
532+
* By default the Sprites will have their `exists` property set to `false`, and they will be
533+
* positioned at 0x0, relative to the `Group.x / y` values.
534+
*
535+
* If `Group.enableBody` is set, then a physics body will be created on the objects, so long as one does not already exist.
536+
*
537+
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the objects, so long as one does not already exist.
538+
*
530539
* @method Phaser.Group#createMultiple
531540
* @param {integer} quantity - The number of Sprites to create.
532541
* @param {string} key - The Game.cache key of the image that this Sprite will use.
533542
* @param {integer|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
534543
* @param {boolean} [exists=false] - The default exists state of the Sprite.
544+
* @return {array} An array containing all of the Sprites that were created.
535545
*/
536546
Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists) {
537547

538548
if (exists === undefined) { exists = false; }
539549

550+
var children = [];
551+
540552
for (var i = 0; i < quantity; i++)
541553
{
542-
this.create(0, 0, key, frame, exists);
554+
children.push(this.create(0, 0, key, frame, exists));
543555
}
544556

557+
return children;
558+
545559
};
546560

547561
/**
@@ -823,7 +837,11 @@ Phaser.Group.prototype.getByName = function (name) {
823837
};
824838

825839
/**
826-
* Replaces a child of this group with the given newChild. The newChild cannot be a member of this group.
840+
* Replaces a child of this Group with the given newChild. The newChild cannot be a member of this Group.
841+
*
842+
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
843+
*
844+
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
827845
*
828846
* @method Phaser.Group#replace
829847
* @param {any} oldChild - The child in this group that will be replaced.
@@ -863,7 +881,7 @@ Phaser.Group.prototype.replace = function (oldChild, newChild) {
863881
* Will scan up to 4 levels deep only.
864882
*
865883
* @method Phaser.Group#hasProperty
866-
* @param {any} child - The child to check for the existance of the property on.
884+
* @param {any} child - The child to check for the existence of the property on.
867885
* @param {string[]} key - An array of strings that make up the property.
868886
* @return {boolean} True if the child has the property, otherwise false.
869887
*/

0 commit comments

Comments
 (0)