Merge pull request #2577 from LoneStranger/dev

Modifed Group.getClosestTo() and Group.getFurthestFrom() to add optional filter callback
This commit is contained in:
Richard Davey 2016-06-21 22:07:13 +01:00 committed by GitHub
commit 8cde880d98

View file

@ -2207,22 +2207,31 @@ Phaser.Group.prototype.getBottom = function () {
}; };
/** /**
* Get the closest child to given Object. * Get the closest child to given Object, with optional callback to filter children.
* *
* This can be a Sprite, Group, Image or any object with public x and y properties. * This can be a Sprite, Group, Image or any object with public x and y properties.
* *
* 'close' is determined by the distance from the objects `x` and `y` properties compared to the childs `x` and `y` properties. * 'close' is determined by the distance from the objects `x` and `y` properties compared to the childs `x` and `y` properties.
* *
* If callback returns true, the object is considered for distance. If callback is null, then any child will satisfy the criteria.
*
* @method Phaser.Group#getClosestTo * @method Phaser.Group#getClosestTo
* @param {any} object - The object used to determine the distance. This can be a Sprite, Group, Image or any object with public x and y properties. * @param {any} object - The object used to determine the distance. This can be a Sprite, Group, Image or any object with public x and y properties.
* @param {function} [callback=null] - The function that each child will be evaluated against. Each child of the group will be passed to it as its first parameter, with the distance as the second.
* @param {object} [callbackContext] - The context in which the function should be called (usually 'this').
* @return {any} The child closest to given object, or null if no child was found. * @return {any} The child closest to given object, or null if no child was found.
*/ */
Phaser.Group.prototype.getClosestTo = function (object) { Phaser.Group.prototype.getClosestTo = function (object, callback, callbackContext) {
var distance = Number.MAX_VALUE; var distance = Number.MAX_VALUE;
var tempDistance = 0; var tempDistance = 0;
var result = null; var result = null;
if (!callback)
{
callback = function() {return true;};
}
for (var i = 0; i < this.children.length; i++) for (var i = 0; i < this.children.length; i++)
{ {
var child = this.children[i]; var child = this.children[i];
@ -2231,7 +2240,7 @@ Phaser.Group.prototype.getClosestTo = function (object) {
{ {
tempDistance = Math.abs(Phaser.Point.distance(object, child)); tempDistance = Math.abs(Phaser.Point.distance(object, child));
if (tempDistance < distance) if (tempDistance < distance && callback.call(callbackContext, child, tempDistance))
{ {
distance = tempDistance; distance = tempDistance;
result = child; result = child;
@ -2244,22 +2253,31 @@ Phaser.Group.prototype.getClosestTo = function (object) {
}; };
/** /**
* Get the child furthest away from the given Object. * Get the child furthest away from the given Object, with optional callback to filter children.
* *
* This can be a Sprite, Group, Image or any object with public x and y properties. * This can be a Sprite, Group, Image or any object with public x and y properties.
* *
* 'furthest away' is determined by the distance from the objects `x` and `y` properties compared to the childs `x` and `y` properties. * 'furthest away' is determined by the distance from the objects `x` and `y` properties compared to the childs `x` and `y` properties.
* *
* If callback returns true, the object is considered for distance. If callback is null, then any child will satisfy the criteria.
*
* @method Phaser.Group#getFurthestFrom * @method Phaser.Group#getFurthestFrom
* @param {any} object - The object used to determine the distance. This can be a Sprite, Group, Image or any object with public x and y properties. * @param {any} object - The object used to determine the distance. This can be a Sprite, Group, Image or any object with public x and y properties.
* @param {function} [callback=null] - The function that each child will be evaluated against. Each child of the group will be passed to it as its first parameter, with the distance as the second.
* @param {object} [callbackContext] - The context in which the function should be called (usually 'this').
* @return {any} The child furthest from the given object, or null if no child was found. * @return {any} The child furthest from the given object, or null if no child was found.
*/ */
Phaser.Group.prototype.getFurthestFrom = function (object) { Phaser.Group.prototype.getFurthestFrom = function (object, callback, callbackContext) {
var distance = 0; var distance = 0;
var tempDistance = 0; var tempDistance = 0;
var result = null; var result = null;
if (!callback)
{
callback = function() {return true;};
}
for (var i = 0; i < this.children.length; i++) for (var i = 0; i < this.children.length; i++)
{ {
var child = this.children[i]; var child = this.children[i];
@ -2268,7 +2286,7 @@ Phaser.Group.prototype.getFurthestFrom = function (object) {
{ {
tempDistance = Math.abs(Phaser.Point.distance(object, child)); tempDistance = Math.abs(Phaser.Point.distance(object, child));
if (tempDistance > distance) if (tempDistance > distance && callback.call(callbackContext, child, tempDistance))
{ {
distance = tempDistance; distance = tempDistance;
result = child; result = child;