Skip to content

Commit 5b4b41c

Browse files
committed
added functions for getting closest child and farthest child
1 parent 9f28d06 commit 5b4b41c

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

src/core/Group.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,60 @@ Phaser.Group.prototype.getBottom = function () {
19751975

19761976
};
19771977

1978+
/**
1979+
* Get the closest child to given Object/Point/Sprite/Image
1980+
*
1981+
* @method Phaser.Group#getClosestTo
1982+
* @return {any} The child closest to given Object/Point/Sprite/Image
1983+
*/
1984+
Phaser.Group.prototype.getClosestTo = function (object) {
1985+
1986+
if (this.children.length !== 0) {
1987+
var distance = Number.MAX_VALUE,
1988+
tempDistance = 0,
1989+
returnee;
1990+
this.forEachAlive(function(e) {
1991+
1992+
tempDistance = Math.abs(Phaser.Point.distance(object, e));
1993+
1994+
if (tempDistance < distance) {
1995+
distance = tempDistance;
1996+
returnee = e;
1997+
}
1998+
});
1999+
2000+
if (returnee) return returnee;
2001+
}
2002+
2003+
};
2004+
2005+
/**
2006+
* Get the farthest child to given Object/Point/Sprite/Image
2007+
*
2008+
* @method Phaser.Group#getFarthestFrom
2009+
* @return {any} The child farthest from given Object/Point/Sprite/Image
2010+
*/
2011+
Phaser.Group.prototype.getFarthestFrom = function (object) {
2012+
2013+
if (this.children.length !== 0) {
2014+
var distance = 0,
2015+
tempDistance = 0,
2016+
returnee;
2017+
this.forEachAlive(function(e) {
2018+
2019+
tempDistance = Math.abs(Phaser.Point.distance(object, e));
2020+
2021+
if (tempDistance > distance) {
2022+
distance = tempDistance;
2023+
returnee = e;
2024+
}
2025+
});
2026+
2027+
if (returnee) return returnee;
2028+
}
2029+
2030+
};
2031+
19782032
/**
19792033
* Get the number of living children in this group.
19802034
*

0 commit comments

Comments
 (0)