Skip to content

Instantly share code, notes, and snippets.

@MattMcFarland
Created March 3, 2011 21:20
Show Gist options
  • Save MattMcFarland/853607 to your computer and use it in GitHub Desktop.
Save MattMcFarland/853607 to your computer and use it in GitHub Desktop.
Snippet: Function checking if an object is within radius of another.
/**
* Check to see if an object is within radius of another.
* @param x1 object1
* @param y1 object1
* @param x2 object2
* @param y2 object2
* @param radius Size of the radius, which is usually
* @return returns true if object2 is within radius of object 1
* @example if (InRadius(player.x,player.y,enemy.x,enemy.y,100)) trace ("enemy within a 100 pixel radius of player");
*/
public function InRadius(x1:Number, y1:Number, x2:Number, y2:Number,radius:Number):Boolean
{
var magnitude:Number = radius * radius
if ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= magnitude) return true;
else return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment