View comments | RSS feed

MovieClip.globalToLocal()

Availability

Flash Player 5.

Usage

my_mc.globalToLocal(point:Object) : Void

Parameters

point The name or identifier of an object created with the generic Object class. The object specifies the x and y coordinates as properties.

Returns

Nothing.

Description

Method; converts the point object from Stage (global) coordinates to the movie clip's (local) coordinates.

You can extend the methods and event handlers of the MovieClip class by creating a subclass. For more information, see "Assigning a class to a movie clip symbol" in Using ActionScript in Flash.

Example

Add the following ActionScript to a FLA or AS file in the same directory as an image called photo1.jpg:

this.createTextField("coords_txt", this.getNextHighestDepth(), 10, 10, 100, 22);
coords_txt.html = true;
coords_txt.multiline = true;
coords_txt.autoSize = true;
this.createEmptyMovieClip("target_mc", this.getNextHighestDepth());
target_mc._x = 100;
target_mc._y = 100;
target_mc.loadMovie("photo1.jpg");

var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
   var point:Object = {x:_xmouse, y:_ymouse};
   target_mc.globalToLocal(point);
   var rowHeaders = "<b> &nbsp; \t</b><b>_x\t</b><b>_y</b>";
   var row_1 = "_root\t"+_xmouse+"\t"+_ymouse;
   var row_2 = "target_mc\t"+point.x+"\t"+point.y;
   coords_txt.htmlText = "<textformat tabstops='[100, 150]'>";
   coords_txt.htmlText += rowHeaders;
   coords_txt.htmlText += row_1;
   coords_txt.htmlText += row_2;
   coords_txt.htmlText += "</textformat>";
};
Mouse.addListener(mouseListener);

See also

MovieClip.getBounds(), MovieClip.localToGlobal()

Comments


Francis Cheng said on Apr 5, 2005 at 11:13 AM :
The MovieClip.globalToLocal() method allows you to convert any given x and y coordinates from values that are relative to the top-left corner of the main stage to values that are relative to the top-left corner of a specific movie clip.

You start by creating 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 over to the right the point lays. For example, if x = 50, then 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 lays. For example, if y = 20, then 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 in one fell swoop with a literal Object value.

var myPoint:Object = {x:50, y:20};

The next step is to convert these global 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 will be (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. It makes sense that 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).

You may have noticed that 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. Here's the code:

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

Now you can try to use this method with a generic object that isn't just sitting in the top-left corner of the stage. Let's go back to our first example where x=50 and y=20, so you are going to convert the global coordinates (x:50, y:20) to local coordinates that are relative to your movie clip. If you create a movie clip that is positioned so that the top left corner of the movie clip is precisely at the global coordinates where x = 50 and y = 20, then the x and y properties of your myPoint object will both be converted to 0 by globalToLocal(). This is because the global values (x:50, y:20) are converted to local values (x:0, y:0).

var myPoint:Object = {x:50, y:20}; // create your generic point object

this.createEmptyMovieClip("myMovieClip", this.getNextHighestDepth());
myMovieClip._x = 50; // _x for movieclip x position
myMovieClip._y = 20; // _y for movieclip y position

myMovieClip.globalToLocal(myPoint);
trace ("x: " + myPoint.x); // output: 0
trace ("y: " + myPoint.y); // output: 0

The globalToLocal() method allows you to determine where a given global coordinate lies relative to the top-left corner of your movie clip. So using the previous example, where does the global point (x:60, y:30) appear in myMovieClip? It appears at local coordinates (10, 10).

var myPoint:Object = {x:60, y:30}; // create your generic point object

this.createEmptyMovieClip("myMovieClip", this.getNextHighestDepth());
myMovieClip._x = 50; // _x for movieclip x position
myMovieClip._y = 20; // _y for movieclip y position

myMovieClip.globalToLocal(myPoint);
trace ("x: " + myPoint.x); // output: 10
trace ("y: " + myPoint.y); // output: 10

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001511.html