globalToLocal (MovieClip.globalToLocal method)
public globalToLocal(pt:Object) : Void
Converts the
object from Stage (global) coordinates to the movie clip's (local) coordinates.
pt
The
method allows you to convert any given x and y
MovieClip.globalToLocal()
coordinates from values that are relative to the top-left corner of the Stage to values that are
relative to the top-left corner of a specific movie clip.
You must first create a generic object that has two properties, x and y. These
and
values
x
y
(and they must be called
and
) are called the global coordinates because they relate to the
x
y
top-left corner of the Stage. The x property represents the horizontal offset from the top-left
corner. In other words, it represents how far to the right the point lies. For example, if x = 50,
the point lies 50 pixels to the right of the top-left corner. The y property represents the vertical
offset from the top-left corner. In other words, it represents how far down the point lies. For
example, if y = 20, the point lies 20 pixels below the top-left corner. The following code
creates a generic object with these coordinates:
var myPoint:Object = new Object();
myPoint.x = 50;
myPoint.y = 20;
Alternatively, you can create the object and assign the values at the same time with a literal
Object value:
var myPoint:Object = {x:50, y:20};
After you create a point object with global coordinates, you can convert the coordinates to
local coordinates. The
method doesn't return a value because it changes
globalToLocal()
the values of
and
in the generic object that you send as the parameter. It changes them
x
y
from values relative to the Stage (global coordinates) to values relative to a specific movie clip
(local coordinates).
For example, if you create a movie clip that is positioned at the point
, and
(_x:100, _y:100)
you pass the global point representing the top-left corner of the Stage
to the
(x:0, y:0)
method, the method should convert the
and
values to the local
globalToLocal()
x
y
coordinates, which in this case is
. This is because the x and y coordinates
(x:-100, y:-100)
are now expressed relative to the top-left corner of your movie clip rather than the top-left
corner of the Stage. The values are negative because to get from the top-left corner of your
movie clip to the top-left corner of the Stage you have to move 100 pixels to the left (negative
x) and 100 pixels up (negative y).
874
ActionScript classes
Need help?
Do you have a question about the FLASH 8-ACTIONSCRIPT 2.0 LANGUAGE and is the answer not in the manual?
Questions and answers