Mathematical functions within Flash seems to be an area that most people like to stay well clear of. Of course, this area of Flash can be very daunting and difficult to get a hold of, so to ease you in, I thought I'd write a tutorial that takes a mathematical theory that we all know and mix it up with just enough Actionscript to help you into the thought-provoking world of maths in Flash without straying too far away from the graphical advantages it also offers.
Roll your mouse around the area below to see what I'm going to help you create:
[ an example of what you will create ]
Can you see what's happening? The transparency (alpha) of the different movieclips on the stage are changing depending on how close your mouse is to them! Pretty neat, eh?
Ok, fire up Flash and let's get crackin:

[ if you can't see the property inspector, hit Control+F3 ]

[ bring up the align panel by hitting Control+K ]
The movieclip you have just created isn't actually going to contain anything! Instead, we have created it so we can use a wicked piece of Actionscript called "onClipEvent (enterFrame)" (which is only available to movieclips). This allows us to have a set of commands executed over and over, each time the playhead of that movieclip is reached.
So you made it this far then? Ok, let's get down to some coding. I promise to make it as easy as possible so don't sweat it.
At this point, you should have a movieclip called "circle" and inside it, you'll have a circle which is 70x70 and centered on the stage. You should also have an instance of the "distancing" movieclip inside here too. Here's what mine looks like:

[ the "circle" movieclip ]
Moving on:onClipEvent (enterFrame)
{
xdist =
Math.round(_root._xmouse
- _parent._x);
ydist =
Math.round(_root._ymouse
- _parent._y);
distancefromthis
= Math.round(Math.sqrt((xdist*xdist)
+ (ydist*ydist)));
_parent._alpha
= (100-distancefromthis)+20;
Do you recognize the formula used in fourth line? It's Pythagoras' theorem! What's that you say? Who's Pythagoras!? You should be ashamed! Never worry, here's what it is, how it works and how it applies here...
Basically, Pythagoras' theory can be used to calculate the length of a side of a right-angled triangle given that we already know the two other sides. Here's a diagram:

[ it has to be a right-angled triangle ]
Now let's imagine that our circle was at the top-left point of that triangle and the user's mouse was at the bottom-right point of the triangle, using that calculation and finding out "z" would give us the distance between the two! So let's figure out each of "x" and "y" individually then...
First of all, "x" is the difference between the mouse's x value and the circle's x value. Therefore, we use the line:
xdist =
Math.round(_root._xmouse
- _parent._x);
Now for the "y" value. I think you can guess that it's the difference between the mouse's y value and the circle's y value. Therefore, we use the Actionscript:
ydist =
Math.round(_root._ymouse
- _parent._y);
We now have the distance of the x co-ordinates in the "xdist" variable and the distance between the y co-ordinates in the "ydist" variable. Let's throw these details into Pythagoras' theory in the third line of our code. The distance between the mouse and the circle (or "z") will be stored in a variable called "distancefromthis". It will contain the square root of the "xdist" variable squared plus the "ydist" variable squared. In Actionscript, this looks like this:
distancefromthis =
Math.round(Math.sqrt((xdist*xdist)
+ (ydist*ydist)));
Notice that I have used the Math.round() function. This just rounds our result up or down to make everything a little tidier.
Phew! I hope you're still with me--that was a lot to take in so pat yourself on the back. The final line of code which actually uses all our hard-earned data and affects the circle with it:
_parent._alpha
= (100-distancefromthis)+20;
Again, the use of the keyword _parent means that we're affecting the circle. Then, we point to the alpha property and set it to full (100) and then subtract the "distancefromthis" variable that we figured out and, as an option, I have added 20. This 20 number is something you should play about with to make things look how you want them. You can even try messing around with the entirety of this line!

[ clicking this from anywhere will bring you to the root ]
And drag a bunch of the "circle" movieclips onto the stage. Test your movie and hey-presto! You should have the desired effect!I hope this tutorial was of decent clarity to you as it is my first. Using this equation, you can really accomplish some great effects in Flash. How about having your script find out the distance between two characters in a platform game and have them react differently to each other depending on their proximity? As always with Flash, your imagination is the only limit.Below you can download the source code for this project. As an added bonus, my Flash file contains a little extra code on the first frame of the root timeline which actually places all your circles on the stage dynamically using the attachMovie() function.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.
Your support keeps this site going! 😇
:: Copyright KIRUPA 2026 //--