diff --git a/src/core/Group.js b/src/core/Group.js index 37a20e4dd..2fcd558f2 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -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.