mirror of
https://github.com/photonstorm/phaser
synced 2024-11-16 09:48:18 +00:00
Merge pull request #1187 from codevinsky/feature/group-filter
Group.filter
This commit is contained in:
commit
bc16c7876f
1 changed files with 31 additions and 1 deletions
|
@ -795,7 +795,7 @@ Phaser.Group.prototype.checkProperty = function (child, key, value, force) {
|
|||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (Phaser.Utils.getProperty(child, key) !== value)
|
||||
{
|
||||
return false;
|
||||
|
@ -1205,6 +1205,36 @@ Phaser.Group.prototype.postUpdate = function () {
|
|||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Allows you to obtain a Phaser.ArrayList of children that return true for the given predicate
|
||||
* For example:
|
||||
* var healthyList = Group.filter(function(child, index, children) {
|
||||
* return child.health > 10 ? true : false;
|
||||
* }, true);
|
||||
* healthyList.callAll('attack');
|
||||
* Note: Currently this will skip any children which are Groups themselves.
|
||||
* @method Phaser.Group#filter
|
||||
* @param {function} predicate - The function that each child will be evaluated against. Each child of the Group will be passed to it as its first parameter, the index as the second, and the entire child array as the third
|
||||
* @param {boolean} [checkExists=false] - If set only children with exists=true will be passed to the callback, otherwise all children will be passed.
|
||||
* @return {Phaser.ArrayList} Returns an array list containing all the children that the predicate returned true for
|
||||
*/
|
||||
Phaser.Group.prototype.filter = function(predicate, checkExists) {
|
||||
var index = -1,
|
||||
length = this.children.length,
|
||||
result = new Phaser.ArrayList();
|
||||
|
||||
while(++index < length) {
|
||||
var child = this.children[index];
|
||||
if(!checkExists || (checkExists && child.exists)) {
|
||||
if(predicate(child, index, this.children)) {
|
||||
result.add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows you to call your own function on each member of this Group. You must pass the callback and context in which it will run.
|
||||
* After the checkExists parameter you can add as many parameters as you like, which will all be passed to the callback along with the child.
|
||||
|
|
Loading…
Reference in a new issue