Game Development Reference
In-Depth Information
AS3.0 has a built-in method called I]pd*]p]j. that will give you the correct angle of rotation between
two objects. One little technical detail you need to deal with, though, is that I]pj*]p]j. returns the
value of the angle in radians , and Movie Clip (and Sprite) objects in AS3.0 don't use radians for their
rotation values; they use degrees. So once you've got the value in radians, you need to apply a very
simple calculation to convert it into degrees. When you have that value, you can rotate the object.
Let me show you just how easy this all is. Figure 9-19 is a grid showing the positions of two objects on
the stage.
X
0
1
2
3
4
5
6
1
2
Y
3
4
5
Figure 9-19. Find the angle of rotation between two objects.
Here's how to find out the angle of rotation, from the point of view of the frog:
1. Find the positions of the objects:
_]p*t91
_]p*u9-
bnkc*t9-
bnkc*u90
2. Subtract the t and u positions of the target object, from the t and u position of the object that
will be doing the rotating:
bnkc*t)_]p*t9)0
bnkc*u)_]p*u9/
3. Plug these numbers into the I]pd*]p]j. function. Very importantly, the u value has to come
first:
I]pd*]p]j.\$/()0%