3/23/2011

All About Angles

Learn about how Flash works with rotation and a code snippet for rotating towards a point.

To work with rotation and angles in Flash, one must first understand how rotation works for display objects in Flash. It uses degrees with a range of -180 to 180 where 0 is up/north. Fortunately, you can assign it any number and Flash will convert it automatically (e.g. 270° = -90°). See the top part of the following swf for a better understanding.

Once you get how rotation works, rotating an object towards a desired point is just a matter of basic trigonometry. I will not go into too much detail about that, but it's basically the inverse tangent (arc tangent) of x and y distance to the point. Be aware that atan returns in radians, so it needs to be converted to degrees. Depending on where the graphic is pointing, you might need to modify the final rotation to get it to point correctly (+90 at the end in the example)

The following swf shows the code of having a object rotate towards the position of the mouse.

No comments:

Post a Comment