Distance formula (I think its 9th grade math, but I’m not sure)
Basic knowledge of AS2 (actionscript 2.0) (esp. hitTests)
OK, so a common problem in all Flash AS2 programming is hitTests between circles. Using the standard hitTest method built in to flash, it will only recognize the squares that the circles would be inscribed in. So these two circles:
http://www.truploader.com/view/284196
Would return a value of true. If you don’t know what I’m talking about, go read a hitTest tut somewhere else, I’m not going to explain it any further.
But how do we get it so that it will only return if they actually ARE touching? There are multiple ways of doing this, and I’m going to show you one of them.
As a refresher, all circles have this nifty little thing called a radius, which is a line segment based off of the origin of the circle that is just long enough so that, if rotated, can touch every part of the circle. The line in this is a radius:
http://www.truploader.com/view/122864
I’m not going into this any further. So basically, my method of circle hitTests is using the distance formula to find the sum of 2 circles’ radii and comparing them. So first off, open up a flash as2 document and create 2 circles (make sure they're perfect circles by holding down shift while drawing them. It won't work otherwise). make them both MC's and give one the instance name of "hero" and the other "enemy". Both of their registration points must be in the middle. Also create a dynamic text box, and give it the instance name of "hit". You're going to need to put this code into both of the circles:
onClipEvent(load){
radius=_width/2;
}
this will find the radius. Since the width would be the diameter (you do the math yourself on that one) half of the width is the radius. Also, just for fun, put this code into your "hero" circle:
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
this._y--;
}
if(Key.isDown(Key.DOWN)){
this._y++;
}
if(Key.isDown(Key.LEFT)){
this._x--;
}
if(Key.isDown(Key.RIGHT)){
this._x++;
}
}
This is just a standard movement code. If you don't understand it, you're in the wrong thread.
Now for the really fun part. The formula. You need to find:
square root of ((difference of x values)squared + (difference of y values)squared). Not so hard, is it?
So here's the code you need in your "hero" MC:
if(Math.sqrt(Math.pow(_x-_root.enemy._x, 2)+Math.pow(_y-_root.enemy._y, 2))<=radius+_root.enemy.radius){
_root.hit.text="hit";
}else{
_root.hit.text="no hit";
}
In case it went over your head, that first conditional is the distance formula put into AS terms. The Math.pow( thing raises everything inside of it to the power that is after the comma, in this case 2. Test it out (preferably at 120 fps), and, unless I missed something, it should work.
This is the final product:
http://www.truploader.com/view/346040
Ps. I'm not giving you the whole code, so you might actually have to figure part of it out on your own :D