mirror of
https://github.com/photonstorm/phaser
synced 2025-03-09 01:37:39 +00:00
Merge pull request #2504 from Nuuf/master
added functions for getting closest child and farthest child
This commit is contained in:
commit
f67d304e99
1 changed files with 63 additions and 6 deletions
|
@ -1975,6 +1975,63 @@ 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) {
|
||||
|
||||
if (this.children.length !== 0) {
|
||||
var distance = Number.MAX_VALUE,
|
||||
tempDistance = 0,
|
||||
returnee;
|
||||
this.forEachAlive(function(e) {
|
||||
|
||||
tempDistance = Math.abs(Phaser.Point.distance(object, e));
|
||||
|
||||
if (tempDistance < distance) {
|
||||
distance = tempDistance;
|
||||
returnee = e;
|
||||
}
|
||||
});
|
||||
|
||||
if (returnee) {
|
||||
return returnee
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
if (this.children.length !== 0) {
|
||||
var distance = 0,
|
||||
tempDistance = 0,
|
||||
returnee;
|
||||
this.forEachAlive(function(e) {
|
||||
|
||||
tempDistance = Math.abs(Phaser.Point.distance(object, e));
|
||||
|
||||
if (tempDistance > distance) {
|
||||
distance = tempDistance;
|
||||
returnee = e;
|
||||
}
|
||||
});
|
||||
if (returnee) {
|
||||
return returnee
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the number of living children in this group.
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue