MovieClip.localToGlobal( ) Method | Flash 5 |
convert a point in a clip to main Stage coordinates |
A reference to an object with two properties, x and y, that describe a point in mc's coordinate space. Both x and y can be any floating-point number.
The localToGlobal( ) method converts the x and y properties of point from coordinates given in mc's coordinate space to coordinates on the main Stage of the Player. Note that localToGlobal( ) does not return a new object; it merely modifies the existing x and y values of point. It has no effect when invoked on a main movie object (i.e., _root), because the original and target coordinate spaces are identical.
To use localToGlobal( ), we must first create a so-called point or coordinates object with x and y properties. To do so, create a generic object from the Object class, and add x and y properties to it:
myPoint = new Object(); myPoint.x = 10; myPoint.y = 20;
The x and y properties of our object are positions on the horizontal and vertical axes of mc, relative to mc's registration point (shown as a crosshair in mc's Library symbol). For example, an x of 10 is 10 pixels to the right of mc's registration point, and a y of 20 is 20 pixels below mc's registration point. With our object created and our x and y properties set, we then pass the object to the localToGlobal( ) method, as in:
myClip.localToGlobal(myPoint);
When localToGlobal( ) is executed, the values of myPoint's x and y properties are transformed to represent the corresponding point on the main Stage, measured from the Stage's upper-left corner. By examining the new values of our myPoint object's properties, we answer the question, "Where does the point (x,y) of the movie clip appear on the main Stage?" For example:
mainX = myPoint.x; mainY = myPoint.y;
The following example determines the location of a clip's registration point, relative to the main Stage:
pt = new Object(); // Create generic object to hold our point pt.x = 0; // Horizontal registration point of clip pt.y = 0; // Vertical registration point of clip this.localToGlobal(pt); // Convert pt to main Stage coordinates trace("On the main Stage, the registration point of the current clip is at: "); trace("x: " + pt.x + "y: " + pt.y);
The localToGlobal( ) method can be used to convert a point to Stage coordinates for use with the hitTest( ) method, which expects points to be supplied in the Stage's coordinate space. It can also be used to compare points in two different movie clips using a common coordinate space.
MovieClip.globalToLocal( ), MovieClip.hitTest( )