A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Mathematics...

  1. #1
    Junior Member
    Join Date
    Jul 2002
    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?

  2. #2
    Senior Member
    Join Date
    Sep 2001
    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,

  3. #3
    Senior Member
    Join Date
    Sep 2001
    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

  4. #4
    Junior Member
    Join Date
    Jul 2002
    Thanks for your help!

    Here's the final script:

    this.newalpha = (_xmouse*_xmouse + _ymouse*_ymouse);
    this.newalpha = this.newalpha/2;
    this._alpha = this.newalpha;

    the dividing by two is just so the square starts fading sooner.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center