Refactored Group.getClosestTo and Group.getFurthestFrom.

This commit is contained in:
photonstorm 2016-06-02 15:11:23 +01:00
parent 97905e0021
commit dfa8b9dedc
2 changed files with 56 additions and 37 deletions

View file

@ -1976,60 +1976,77 @@ Phaser.Group.prototype.getBottom = function () {
};
/**
* Get the closest child to given Object/Point/Sprite/Image
*
* @method Phaser.Group#getClosestTo
* @return {any} The child closest to given Object/Point/Sprite/Image
*/
Phaser.Group.prototype.getClosestTo = function(object) {
* Get the closest child to given Object.
*
* 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.
*
* @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.
* @return {any} The child closest to given object, or null if no child was found.
*/
Phaser.Group.prototype.getClosestTo = function (object) {
if (this.children.length !== 0) {
var distance = Number.MAX_VALUE,
tempDistance = 0,
returnee;
this.forEachAlive(function(e) {
var distance = Number.MAX_VALUE;
var tempDistance = 0;
var result = null;
tempDistance = Math.abs(Phaser.Point.distance(object, e));
for (var i = 0; i < this.children.length; i++)
{
var child = this.children[i];
if (tempDistance < distance) {
if (child.exists)
{
tempDistance = Math.abs(Phaser.Point.distance(object, child));
if (tempDistance < distance)
{
distance = tempDistance;
returnee = e;
result = child;
}
});
if (returnee) {
return returnee
};
}
}
return result;
};
/**
* Get the farthest child from given Object/Point/Sprite/Image
*
* @method Phaser.Group#getFarthestFrom
* @return {any} The child farthest from given Object/Point/Sprite/Image
*/
Phaser.Group.prototype.getFarthestFrom = function(object) {
* Get the child furthest away from the given Object.
*
* 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.
*
* @method Phaser.Group#getFarthestFrom
* @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.
* @return {any} The child furthest from the given object, or null if no child was found.
*/
Phaser.Group.prototype.getFarthestFrom = function (object) {
if (this.children.length !== 0) {
var distance = 0,
tempDistance = 0,
returnee;
this.forEachAlive(function(e) {
var distance = 0;
var tempDistance = 0;
var result = null;
tempDistance = Math.abs(Phaser.Point.distance(object, e));
for (var i = 0; i < this.children.length; i++)
{
var child = this.children[i];
if (tempDistance > distance) {
if (child.exists)
{
tempDistance = Math.abs(Phaser.Point.distance(object, child));
if (tempDistance > distance)
{
distance = tempDistance;
returnee = e;
result = child;
}
});
if (returnee) {
return returnee
};
}
}
return result;
};
/**

View file

@ -1707,9 +1707,11 @@ declare module "phaser" {
getAt(index: number): PIXI.DisplayObject | number;
getBottom(): any;
getByName(name: string): any;
getClosestTo(object: any): any;
getFirstAlive(createIfNull?: boolean, x?: number, y?: number, key?: string | Phaser.RenderTexture | Phaser.BitmapData | Phaser.Video | PIXI.Texture, frame?: string | number): any;
getFirstDead(createIfNull?: boolean, x?: number, y?: number, key?: string | Phaser.RenderTexture | Phaser.BitmapData | Phaser.Video | PIXI.Texture, frame?: string | number): any;
getFirstExists(exists: boolean, createIfNull?: boolean, x?: number, y?: number, key?: string | Phaser.RenderTexture | Phaser.BitmapData | Phaser.Video | PIXI.Texture, frame?: string | number): any;
getFurthestFrom(object: any): any;
getIndex(child: any): number;
getRandom(startIndex?: number, length?: number): any;
getTop(): any;