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..
-Smoothing effec..
-Array functions..
-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: 16455 | rating: 7/10 | downloads: 2497 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

0-360 ? again
posted by: Gary
on: Sep 22, 08 5: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 8: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)



0-360?
posted by: Jason
on: Jul 17, 07 7:41 pm

What would we have to do to get this to display 0-360, instead of negative values?

The reason I ask is because I'd like assign a 'go to' value to the result so that I could successful revolve around an object, that I've animated, and assigned to a 360 frame movie clip???

post reply | read replies (1)



incredible
posted by: Jeff
on: Aug 21, 07 10:55 pm

luved it. best angle detecting tut on the web (as far as i could find)

post reply | read replies (0)



Super...
posted by: vijay
on: Aug 10, 07 12:34 am

Tnks for this tips... Give More 3D angles...

post reply | read replies (0)



read more commentsread more comments   |   read more commentspost comment 



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