Get nearest entity
findNearestEntity: function() {
    var nearestDistance = Infinity;
    var nearestEntity = null;
    for( var i = 0; i < ig.game.entities.length; i++ ) {
        var ent = ig.game.entities[i];
        var distance = this.distanceTo( ent );
        if( distance < nearestDistance && ent != this ) {
            nearestDistance = distance;
            nearestEntity = ent;
        }
    }
    return nearestEntity;
}Written by Pablo Damian Cotoia
Related protips
1 Response
Sin loop:
var that = this;
return Math.min.apply(ig.game.entities.map(function () {
    return this.distanceTo(that);
}));
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
 #Impact game engine 
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#
