Skip to content

Commit 821fff0

Browse files
committed
Group fixes/changes:
- remove() rejects non-members - createMultiple() stops creating objects if the Group becomes full - isFull() returns true if the group size *exceeds* maxSize - Group() calls createMultiple() only if config.key was passed
1 parent e8d50a9 commit 821fff0

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

src/gameobjects/group/Group.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var Sprite = require('../sprite/Sprite');
4949
*
5050
* If `max` is positive, then the total created will not exceed `max`.
5151
*
52-
* `key` is required.
52+
* `key` is required. {@link Phaser.GameObjects.Group#defaultKey} is not used.
5353
*
5454
* @property {?object} [classType] - The class of each new Game Object.
5555
* @property {string} [key] - The texture key of each new Game Object.
@@ -105,7 +105,7 @@ var Sprite = require('../sprite/Sprite');
105105
* @since 3.0.0
106106
* @param {Phaser.Scene} scene - The scene this group belongs to.
107107
* @param {?(Phaser.GameObjects.GameObject[]|GroupConfig)} [children] - Game objects to add to this group; or the `config` argument.
108-
* @param {GroupConfig} [config] - Settings for this group.
108+
* @param {GroupConfig|GroupCreateConfig} [config] - Settings for this group. If `key` is set, Phaser.GameObjects.Group#createMultiple is also called with these settings.
109109
*
110110
* @see Phaser.Physics.Arcade.Group
111111
* @see Phaser.Physics.Arcade.StaticGroup
@@ -183,6 +183,9 @@ var Group = new Class({
183183
/**
184184
* A default texture key to use when creating new group members.
185185
*
186+
* This is used in {@link Phaser.GameObjects.Group#create}
187+
* but not in {@link Phaser.GameObjects.Group#createMultiple}.
188+
*
186189
* @name Phaser.GameObjects.Group#defaultKey
187190
* @type {string}
188191
* @since 3.0.0
@@ -236,7 +239,7 @@ var Group = new Class({
236239
*/
237240
this.createMultipleCallback = GetFastValue(config, 'createMultipleCallback', null);
238241

239-
if (config)
242+
if (config && config.key !== undefined)
240243
{
241244
this.createMultiple(config);
242245
}
@@ -294,6 +297,8 @@ var Group = new Class({
294297
/**
295298
* Creates several Game Objects and adds them to this group.
296299
*
300+
* If the group becomes {@link Phaser.GameObjects.Group#isFull}, no further Game Objects are created.
301+
*
297302
* Calls {@link Phaser.GameObjects.Group#createMultipleCallback}
298303
* and {@link Phaser.GameObjects.Group#createCallback}.
299304
*
@@ -306,6 +311,11 @@ var Group = new Class({
306311
*/
307312
createMultiple: function (config)
308313
{
314+
if (this.isFull())
315+
{
316+
return [];
317+
}
318+
309319
if (!Array.isArray(config))
310320
{
311321
config = [ config ];
@@ -335,6 +345,11 @@ var Group = new Class({
335345
*/
336346
createFromConfig: function (options)
337347
{
348+
if (this.isFull())
349+
{
350+
return [];
351+
}
352+
338353
this.classType = GetFastValue(options, 'classType', this.classType);
339354

340355
var key = GetFastValue(options, 'key', undefined);
@@ -384,7 +399,14 @@ var Group = new Class({
384399

385400
for (var c = 0; c < range.length; c++)
386401
{
387-
entries.push(this.create(0, 0, range[c].a, range[c].b, visible, active));
402+
var created = this.create(0, 0, range[c].a, range[c].b, visible, active);
403+
404+
if (!created)
405+
{
406+
break;
407+
}
408+
409+
entries.push(created);
388410
}
389411

390412
// Post-creation options (applied only to those items created in this call):
@@ -555,6 +577,11 @@ var Group = new Class({
555577
{
556578
if (removeFromScene === undefined) { removeFromScene = false; }
557579

580+
if (!this.children.contains(child))
581+
{
582+
return this;
583+
}
584+
558585
this.children.delete(child);
559586

560587
if (this.removeCallback)
@@ -825,7 +852,7 @@ var Group = new Class({
825852
}
826853
else
827854
{
828-
return (this.children.size === this.maxSize);
855+
return (this.children.size >= this.maxSize);
829856
}
830857
},
831858

0 commit comments

Comments
 (0)