View comments | RSS feed

MovieClip.localToGlobal()

Availability

Flash Player 5.

Usage

my_mc.localToGlobal(point:Object) : Void

Parameters

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

Returns

Nothing.

Description

Method; converts the point object from the movie clip's (local) coordinates to the Stage (global) 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

The following example converts x and y coordinates of the my_mc object, from the movie clip's (local) coordinates to the Stage (global) coordinates. The center point of the movie clip is reflected after you click and drag the instance.

this.createTextField("point_txt", this.getNextHighestDepth(), 0, 0, 100, 22);
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
   var point:Object = {x:my_mc._width/2, y:my_mc._height/2};
   my_mc.localToGlobal(point);
   point_txt.text = "x:"+point.x+", y:"+point.y;
};
Mouse.addListener(mouseListener);
my_mc.onPress = function() {
   this.startDrag();
};
my_mc.onRelease = function() {
   this.stopDrag();
};
}

See also

MovieClip.globalToLocal()

Comments


MotionMaker said on Sep 22, 2004 at 12:43 PM :
Extra close curly bracket. Although this example was not intended to be complete, it still has one too many close curly backets. Delete the last close curly backet and have fun with applying to an example.
------------------------------------
Here is an expansion on the example to give you an idea how to use it.

this.createEmptyMovieClip("my_mc", this.getNextHighestDepth());
with (my_mc)
{
lineStyle(1, 0x000000, 100);
beginFill(0xffff00, 100);
moveTo(0, 0);
lineTo(0, 100);
lineTo(100, 100);
lineTo(100, 0);
lineTo(0, 0);
endFill()
_x = 100
_y = 100
}


this.createTextField("point_txt", this.getNextHighestDepth(), 0, 0, 100, 22);
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
var point:Object = {x:my_mc._width/2, y:my_mc._height/2};
my_mc.localToGlobal(point);
point_txt.text = "x:"+point.x+", y:"+point.y;
};
Mouse.addListener(mouseListener);
my_mc.onPress = function() {
this.startDrag();
};
my_mc.onRelease = function() {
this.stopDrag();
};
Chrominance said on Nov 24, 2004 at 7:45 AM :
// Remember: The _x and _y properties of a movie clip represent the
// 0,0 coordinate of the movie clip, in terms of the _parent timeline.

// To get the _x and _y coordinates of a movie-clip, in terms of
// the global coordinate system, the point you're looking to convert
// is in fact the 0,0 coordinate of the movie clip.

// I was getting hung up trying to pass the _x and _y properties
// of the movie clip for coordinate conversion.

// Where: target_mc is a reference to some movie-clip,
// anywhere in a flash project.

var point: Object = {x:0,y:0};
target_mc.localToGlobal(point);

globalXCoord = point.x;
globalYCoord = point.y;
mab123 said on Apr 27, 2005 at 10:53 AM :
I get very strange results with the localToGlobal and globalToLocal APIs. Because the hitArea movie clip property doesn't seem to work for me, I resorted to writing my own. So I'd like to do this:

function setHitArea ( movieClip )
{
// We would like to use the hitArea property of the movie clip but it
// doesn't seem to work so we make a hitArea movie and use it.
//
// this.thumbnailMovie.hitArea = movieClip;

if ( ! movieClip )
{
movieClip = this.thumbnailMovie;
}

var origin = { x: movieClip._width / 2, y: movieClip._height / 2 };
trace ( 'o1: ' + origin.x + ', ' + origin.y );
movieClip.localToGlobal ( origin );
trace ( 'o2: ' + origin.x + ', ' + origin.y );
this.hitAreaMovie.globalToLocal ( origin );
trace ( 'o3: ' + origin.x + ', ' + origin.y );
this.hitAreaMovie._x = origin.x - movieClip._width / 2;
this.hitAreaMovie._y = origin.y - movieClip._height / 2;
this.hitAreaMovie._width = movieClip._width;
this.hitAreaMovie._height = movieClip._height;
}

But when I run it I get traces like this:

o1: 370, 5
o2: 27430, 293
o3: 352.7, 158.35

Now the movie clip I pass in is visible in the document which is 840 x 500 and so o2 should be a point within 840 x 500 but it's not. Also, the hit area movie has its origin and its parent origin and its parent origin... set at integer boundaries and so how the .7 and .35 got in there I have no idea.
Thais Derich said on Jul 20, 2005 at 11:45 AM :
Thank you MotionMaker. The source has been updated.

 

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/00001521.html