MovieClip
475
You must first create a generic object that has two properties, x and y. These x and y values
(and they must be called x and y) are called the global coordinates because they relate to the
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
globalToLocal()
method doesn't return a value because it changes
the values of x and y in the generic object that you send as the parameter. It changes them
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
(_x:100, _y:100)
, and
you pass the global point representing the top-left corner of the Stage
(x:0, y:0)
to the
globalToLocal()
method, the method should convert the x and y values to the local
coordinates, which in this case is
(x:-100, y:-100)
. This is because the x and y coordinates
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).
The movie clip coordinates were expressed using _x and _y, because those are the MovieClip
properties that you use to set the x and y values for MovieClips. However, your generic object
uses x and y without the underscore. The following code converts the
x
and
y
values to the
local coordinates:
var myPoint:Object = {x:0, y:0}; // Create your generic point object.
this.createEmptyMovieClip("myMovieClip", this.getNextHighestDepth());
myMovieClip._x = 100; // _x for movieclip x position
myMovieClip._y = 100; // _y for movieclip y position
myMovieClip.globalToLocal(myPoint);
trace ("x: " + myPoint.x); // output: -100
trace ("y: " + myPoint.y); // output: -100
Summary of Contents for FLASHLITE2 ACTIONSCRIPT-LANGUAGE
Page 1: ...Flash Lite 2 x ActionScript Language Reference...
Page 22: ...22 Contents...
Page 244: ...244 ActionScript language elements...
Page 760: ...760 ActionScript classes...