mirror of
https://github.com/photonstorm/phaser
synced 2024-11-16 17:58:23 +00:00
added: Group.filter
takes a predicate function and passes child, index, and the entire child array to it. return an ArrayList containing all children that the predicate returns true for.
This commit is contained in:
parent
bb8da110b1
commit
298622f65f
1 changed files with 32 additions and 1 deletions
|
@ -788,7 +788,7 @@ Phaser.Group.prototype.checkProperty = function (child, key, value, force) {
|
|||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (Phaser.Utils.getProperty(child, key) !== value)
|
||||
{
|
||||
return false;
|
||||
|
@ -1198,6 +1198,37 @@ 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) {
|
||||
* if(child.exists && child.health > 10) {
|
||||
* return 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.
|
||||
*/
|
||||
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