Sheesh, some of you commentors need to give the asker a little more slack. It's not necessarily apparent to a lot of people that this can even be broken down into a problem concerning right-triangles.
Marjin's answer is correct, but you can actually go one step further and be a lot more efficient, which will help the overall performance of your game in the end.
Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
will get you the distance between x1,y1 and x2,y2, but you don't actually need to do the square root part.
You see, you don't really care what the distance is, you just care if its more or less than another distance. So you'll always be comparing two distances, in this case the distance from the formula above you'll be comparing to 200. Since you're comparing distances, you can instead square 200:
Math.pow(200, 2)
to get the distance squared. Then you can use the above formula without the square root:
Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2);
and compare the formula to 40000 instead of 200. The advantage of this is that you only call Math.pow
on 200 once, and in return you never have to call Math.sqrt
ever! Since you'll be checking a lot of creeps many times a second these calls add up and it ought to make your game more performant if you are comparing squared distances instead of distances because of it.
Here's some example code that illustrates how using them are pretty much the same for your purposes. It uses console.log
, and if you don't know how to look at that you should look at the visuals in the first two parts of this tutorial.
Here's the code:
http://jsfiddle.net/4aPjB/