View Full Version : doc's circle hitTest tut [Flash][Actionscript]

06-18-2009, 06:23 PM
What you will need:
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:


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:


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:


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:


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="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:

Ps. I'm not giving you the whole code, so you might actually have to figure part of it out on your own :D

08-05-2009, 09:31 PM
Can someone give me some feedback? Its my first tut and I wanna know what I can do differently

08-06-2009, 05:26 AM
I have no idea what your tutorial is about, but thats just because i am AS disabled. Anyway, it seems well thought out. I wish i could say more. Posting in the tutorial section can be annoying, its not very active.

Good job.

08-07-2009, 11:05 AM
thanks cronos!
Yeah, youre right, it doesn't seem very active. Oh well.
Anyone else?