Skip to content

Commit 9b4bb1b

Browse files
committed
Group.getRandomExists will return a random child from the Group that has exists set to true.
Group.getAll will return all children in the Group, or a section of the Group, with the optional ability to test if the child has a property matching the given value or not. Group.iterate has a new `returnType`: `RETURN_ALL`. This allows you to return all children that pass the iteration test in an array.
1 parent 1467eb4 commit 9b4bb1b

2 files changed

Lines changed: 101 additions & 13 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
309309

310310
### New Features
311311

312-
*
313-
*
314-
*
312+
* Group.getRandomExists will return a random child from the Group that has exists set to true.
313+
* Group.getAll will return all children in the Group, or a section of the Group, with the optional ability to test if the child has a property matching the given value or not.
314+
* Group.iterate has a new `returnType`: `RETURN_ALL`. This allows you to return all children that pass the iteration test in an array.
315315

316316
### Updates
317317

src/core/Group.js

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,13 @@ Phaser.Group.RETURN_TOTAL = 1;
317317
*/
318318
Phaser.Group.RETURN_CHILD = 2;
319319

320+
/**
321+
* A returnType value, as specified in {@link #iterate} eg.
322+
* @constant
323+
* @type {integer}
324+
*/
325+
Phaser.Group.RETURN_ALL = 3;
326+
320327
/**
321328
* A sort ordering value, as specified in {@link #sort} eg.
322329
* @constant
@@ -1996,13 +2003,25 @@ Phaser.Group.prototype.descendingSortHandler = function (a, b) {
19962003
*/
19972004
Phaser.Group.prototype.iterate = function (key, value, returnType, callback, callbackContext, args) {
19982005

1999-
if (returnType === Phaser.Group.RETURN_TOTAL && this.children.length === 0)
2006+
if (this.children.length === 0)
20002007
{
2001-
return 0;
2008+
if (returnType === Phaser.Group.RETURN_TOTAL)
2009+
{
2010+
return 0;
2011+
}
2012+
else if (returnType === Phaser.Group.RETURN_ALL)
2013+
{
2014+
return [];
2015+
}
20022016
}
20032017

20042018
var total = 0;
20052019

2020+
if (returnType === Phaser.Group.RETURN_ALL)
2021+
{
2022+
var output = [];
2023+
}
2024+
20062025
for (var i = 0; i < this.children.length; i++)
20072026
{
20082027
if (this.children[i][key] === value)
@@ -2026,16 +2045,26 @@ Phaser.Group.prototype.iterate = function (key, value, returnType, callback, cal
20262045
{
20272046
return this.children[i];
20282047
}
2048+
else if (returnType === Phaser.Group.RETURN_ALL)
2049+
{
2050+
output.push(this.children[i]);
2051+
}
20292052
}
20302053
}
20312054

20322055
if (returnType === Phaser.Group.RETURN_TOTAL)
20332056
{
20342057
return total;
20352058
}
2036-
2037-
// RETURN_CHILD or RETURN_NONE
2038-
return null;
2059+
else if (returnType === Phaser.Group.RETURN_ALL)
2060+
{
2061+
return output;
2062+
}
2063+
else
2064+
{
2065+
// RETURN_CHILD or RETURN_NONE
2066+
return null;
2067+
}
20392068

20402069
};
20412070

@@ -2322,24 +2351,83 @@ Phaser.Group.prototype.countDead = function () {
23222351
* Returns a random child from the group.
23232352
*
23242353
* @method Phaser.Group#getRandom
2325-
* @param {integer} [startIndex=0] - Offset from the front of the front of the group (lowest child).
2354+
* @param {integer} [startIndex=0] - Offset from the front of the group (lowest child).
23262355
* @param {integer} [length=(to top)] - Restriction on the number of values you want to randomly select from.
23272356
* @return {any} A random child of this Group.
23282357
*/
23292358
Phaser.Group.prototype.getRandom = function (startIndex, length) {
23302359

2331-
if (this.children.length === 0)
2360+
if (startIndex === undefined) { startIndex = 0; }
2361+
if (length === undefined) { length = this.children.length; }
2362+
2363+
if (length === 0)
23322364
{
23332365
return null;
23342366
}
23352367

2336-
startIndex = startIndex || 0;
2337-
length = length || this.children.length;
2338-
23392368
return Phaser.ArrayUtils.getRandomItem(this.children, startIndex, length);
23402369

23412370
};
23422371

2372+
/**
2373+
* Returns a random child from the Group that has `exists` set to `true`.
2374+
*
2375+
* Optionally you can specify a start and end index. For example if this Group had 100 children,
2376+
* and you set `startIndex` to 0 and `endIndex` to 50, it would return a random child from only
2377+
* the first 50 children in the Group.
2378+
*
2379+
* @method Phaser.Group#getRandomExists
2380+
* @param {integer} [startIndex=0] - The first child index to start the search from.
2381+
* @param {integer} [endIndex] - The last child index to search up to.
2382+
* @return {any} A random child of this Group that exists.
2383+
*/
2384+
Phaser.Group.prototype.getRandomExists = function (startIndex, endIndex) {
2385+
2386+
var list = this.getAll('exists', true, startIndex, endIndex);
2387+
2388+
return this.game.rnd.pick(list);
2389+
2390+
};
2391+
2392+
/**
2393+
* Returns all children in this Group.
2394+
*
2395+
* You can optionally specify a matching criteria using the `property` and `value` arguments.
2396+
*
2397+
* For example: `getAll('exists', true)` would return only children that have their exists property set.
2398+
*
2399+
* Optionally you can specify a start and end index. For example if this Group had 100 children,
2400+
* and you set `startIndex` to 0 and `endIndex` to 50, it would return a random child from only
2401+
* the first 50 children in the Group.
2402+
*
2403+
* @method Phaser.Group#getAll
2404+
* @param {string} [property] - An optional property to test against the value argument.
2405+
* @param {any} [value] - If property is set then Child.property must strictly equal this value to be included in the results.
2406+
* @param {integer} [startIndex=0] - The first child index to start the search from.
2407+
* @param {integer} [endIndex] - The last child index to search up until.
2408+
* @return {any} A random existing child of this Group.
2409+
*/
2410+
Phaser.Group.prototype.getAll = function (property, value, startIndex, endIndex) {
2411+
2412+
if (startIndex === undefined) { startIndex = 0; }
2413+
if (endIndex === undefined) { endIndex = this.children.length; }
2414+
2415+
var output = [];
2416+
2417+
for (var i = startIndex; i < endIndex; i++)
2418+
{
2419+
var child = this.children[i];
2420+
2421+
if (property && child[property] === value)
2422+
{
2423+
output.push(child);
2424+
}
2425+
}
2426+
2427+
return output;
2428+
2429+
};
2430+
23432431
/**
23442432
* Removes the given child from this group.
23452433
*

0 commit comments

Comments
 (0)