Game Development Reference
In-Depth Information
Figure 9-18 takes you on a tour of how to convert a subobject's local to global coordinates.
Convert Local Coordinates to Global Coordinates
1. Import the Point class.
2. Create a new Point object.
The x position of the subobject
The y position of the subobject
This is a new Point object
called pointObject.
Point objects are used to store points.
They contain their own x and y properties
that you can refer to like this:
These x and y properties contain the values of whatever points
were specified in the argument when you created the Point object.
3. Create new variables to store the global coordinates for these points.
r]n chk^]hLkejp[U 6Jqi^an9 i]ejK^fa_p*hk_]hPkChk^]h$lkejpK^fa_p%*u7
Variables to store the global points.
Use the parent object's localToGlobal method to convert
the pointObject's x and y properties to global coordinates.
You can now use globalPoint_X and globalPoint_Y
to access the subobject's global x and y position.
Figure 9-18. Convert local x and y coordinates to global x and y coordinates.
Now that you can access the global coordinates of the frog's left and right eyes, you can use these
values to rotate them.
Rotating toward an object
You'll make the frog's eyes follow the lh]uan object as it moves around the stage. To do this, you need
to apply a bit of trigonometry to the eyes' nkp]pekj properties.
If you're a mathophobe, rest assured that you don't need to necessarily understand the trigonometry
you'll look at to be able to use it. If fact, you won't even see it. AS3.0 does the math for you—you
just need to give it the correct numbers. And once you see it in use, you'll see how easy it is to apply
whenever you need to rotate an object toward another object. It's a walk in the park compared with
converting coordinate systems.
Search Nedrilad ::

Custom Search