home |  
Sell Downloads - Ejunkie
open db network by 19.5 degrees
LYRICS | FREE E-BOOKS | SELL DOWNLOADS WITH PAYPAL
 in   
contribute for fun & profit
brink
-Array functions..
-Flash Remoting ..
-str_pad functio..
-Detecting mouse..
-Ltrim, Rtrim an..
-Opening externa..
-Modifying Flash..
-Array functions..
-Smoothing effec..
-Drawing / Vecto..
 
See all Flash Tutorials
 
-Extensions/Plug..
-FLA..
-Flash Software..
-Flash Tutorials..
 
See all Flash
 
All Resources > Flash > Flash Tutorials > ACTIONSCRIPT TUTORIALS
spread the word around  send this page to a friend   read/write comments/corrections/additions comments  rate this 

Detecting mouse angle in flash

by 19.5 Degrees
 
 
views: 22933 | rating: 6/10 | downloads: 3150 download
 




Draw an imaginary triangle using three points. The origin (center of your movie), the mouse, and the third point is where the perpendicular from your mouse intersects the x axis. Now, what we are trying to find is the angle theta, the only way we can find it is using a bit of trigonometry.

Tan (theta) = Opposite side / Adjacent side

theta (angle we are trying to find) = tan -1 (opposite side/adjacent side)

now, we need to find out opposite side and adjacent side, which is easy

adjside = _root._xmouse-_root.origin._x;

oppside = -1*(_root._ymouse-_root.origin._y);

we have multiplied oppside with -1 as flash cartesian system is flipped along x axis (which means y axis is upside down). In normal scenarios, descending down the y axis from top to bottom shows a decrease in y, in flash it increases.

now, we have to find the tan -1, flash has a function Math.atan2() for that

angle = Math.atan2(oppside, adjside);

this function returns the values in radians, we still need to convert it in degrees

angle = Math.round(angle/Math.PI*180);

Finally, just make a movie clip and put the above code in onClipEvent (mouseMove) so flash will calculate the mouse angle for you whenever it changes.

Final code

onClipEvent (mouseMove) {

adjside =_root. _xmouse-_root.origin._x;
oppside = -1*(_root._ymouse-_root.origin._y);

angle = Math.atan2(oppside, adjside); // in radians
angle = Math.round(angle/Math.PI*180); // convert to degrees
_root.compass._rotation = -1*(angle);

}


download


« PREVIOUS
  INDEX
NEXT »

spread the word around
read comments

How do I make multiple instances of this object?
posted by: bob
on: Jun 7, 09 7:38 pm

I want to make multiple instances of this mc. is it possible? how would i go about doing it for all of the instances to have an action relative the position of the mouse? please get back to me soon

thanks.

post reply | read replies (0)



untitled
posted by: .
on: Jan 31, 09 11:29 am

//If you want the angle to be between 0 and 360
if (angle < 0){
аngle += 360;
аngle %= 360; //resetting the angle
}

post reply | read replies (2)



The 0 - 360 thing
posted by: Hock
on: Dec 5, 08 9:09 am

If you want the angle to be between 0 and 360 (no neg. values)

you may do this

if (angle < 0)
angle = 3600+angle


(remember angle is negative so that's why you ADD and not substract the angle)

post reply | read replies (0)



0-360 ? again
posted by: Gary
on: Sep 22, 08 4:54 pm

Adding 180 to my textbox created innapropriate angles, so:
I had an idea to make it be 360.
I added 360 to my entire angle, then I tried to an add if statement that would make any angle < 360 to stay the same, but and angle > 360 to be itself - 360 (so -70 = 290, -25 = 335, 14 still = 14)
But it disreguardes my if statement completely:

if (angle > 360) {
angle = angle-360;
} else {
angle = angle;
}
is there a problem with my if or is it just being mean :'(

post reply | read replies (1)



cool, but flash sux
posted by: joe
on: Apr 22, 08 7:38 am

in a rect triangle (a=5, b=4, c=3) the angles are respectively 90, 60 and 30. but flash can't output the values correctly, unfortunately....

but the tut is spot on! :)

post reply | read replies (1)



read more commentsread more comments   |   read more commentspost comment 



home | contact | contribute | terms of use | privacy policy |