# Mathematics...

• 07-25-2002, 04:56 PM
NeutronicMX
I'm trying to make a movie clip that changes it's alpha depending on how close the mouse is to it's center. So far, all I've gotten is a bunch of failures...here's the script that has worked the bast so far:

//determine number of pixels difference between movieclip and mouse, then set the _alpha of movieclip to half of what is returned.

movieclip.xalpha = Math.abs(movieclip._x - _xmouse);
movieclip.yalpha = Math.abs(movieclip._y - _ymouse);
movieclip.newalpha = movieclip.xalpha + movieclip.yalpha;
movieclip.newalpha = movieclip.newalpha - movieclip._x;
movieclip.newalpha = movieclip.newalpha - movieclip._y;
movieclip.newalpha = movieclip.newalpha / 2;
movieclip.newalpha = Math.abs(movieclip.newalpha);
movieclip._alpha = movieclip.newalpha;

I had to subtract the movieclip's x and y because for some reason when I traced movieclip._alpha, it was always higher than it was supposed to be by those two numbers added together. Now it works mostly (when the mouse gets closer the movieclip fades), but it also fades when the mouse is up and to the right, and down and to the left. Any suggestions?
• 07-26-2002, 02:22 AM
snapcase
The problem is that you are adding the x and y distances to get the "total" distance from the movie clip. Instead, use the pythagorean theorem to find the distance. That is, x^2 + y^2 = z^2 where x is the x distance, y is the y distance, and z is the hypotenuse distance, which is what you are looking for. Oh, and ^2 means "squared", just in case... :)

So, you're code would be something like:

xdistance = Math.abs(movieclip._x - _xmouse);
ydistance = Math.abs(movieclip._y - _ymouse);
zsquared = (xdistance * xdistance) + (ydistance + ydistance);
zdistance = Math.sqrt(zsquared);
movieclip._alpha = (zdistance / 2);

Try it and tell me if it works, maybe I'll use it on a project myself :)

Good luck and have fun,
Snapcase.
• 07-26-2002, 02:26 AM
snapcase
OOPS!! Typo!

In the line for zsquared, it's supposed to write:

zsquared = (xdistance* xdistance) + (ydistance * ydistance);

-------------------- changed this ------------^^^

I changed the second addition to a multiplication, so the above is now correct!

Sorry! :P
• 07-26-2002, 12:11 PM
NeutronicMX