localToGlobal() in AS3 not working
Date: Wednesday 13th May 2009
Time: 17:06
Category: ActionScript 3.0
Views: 1,518
Comments:
localToGlobal() not working? The LiveDocs on this potentially very useful method, along with its partner globalToLocal() are not actually very helpful.
What they don’t make clear is that you need to be sure to overwrite your point when using it:
// WRONG:
var pt:Point = new Point(target.x, target.y);
target.parent.localToGlobal(pt);
parent.globalToLocal(pt);
// RIGHT:
var pt:Point = new Point(target.x, target.y);
pt = target.parent.localToGlobal(pt);
pt = parent.globalToLocal(pt);
Simply running a localToGlobal() method on a point in a given scope is not enough. You need to write the result back into the point. You might be forgiven for not seeing this, because it was not necessary for the equivalent method in AS2!
localToGlobal() and globalToLocal() can be the source of considerable frustration, especially as this differences such as this are not mentioned by Adobe. But properly understood these methods are not complicated. We hope this post has helped you.

/rating_start.png)
/rating_on.png)
/rating_on.png)
/rating_on.png)
/rating_half.png)
/rating_off.png)
(11 votes, average: 3.91 out of 5)
8 Responses to “localToGlobal() in AS3 not working”:
December 10th, 2009 at 11:37 am
Thanks a lot for this post.
December 10th, 2009 at 11:45 am
You’re welcome!
December 21st, 2009 at 6:20 pm
Yes, that was easy after reading your tip. Especially “target.parent…” part helped a lot. Thanks!
December 22nd, 2009 at 5:32 pm
I feel like I am missing something…
I have a Class named ViewPort that is used to mask another Class named Canvas by passing in the reference. The ViewPort object has a child called “maskMC.” The mask is applied to the Canvas object that is referenced. So far so good…
The ViewPort object lives in a hierarchy of other MovieClips that are used to manage layout. The Canvas object to be masked lives outside of this hierarchy.
I am unable to position the masked object at the same position as the ViewPort object? All traces show (x=0, y=0)? Is this related to this post?
Any help would be greatly appreciated!
December 22nd, 2009 at 6:03 pm
Passing the Canvas object in the constructor and trying to get the coordinates was “pointless” ha ha get it… I hadn’t yet added the ViewPort as a child to the container.
I’m glad I stumbled upon this site, you are now bookmarked! Hopefully I’ll have something to contribute soon thanks!
December 23rd, 2009 at 5:57 pm
Hi Brian,
Thanks. Yes, it’s important to note that objects need to be on the Display List before the stage is accessible through them.
Here’s an article where we discuss this in a bit more detail.
Keep an eye on the site via RSS if you like – every time we start a major project “gotchas” crop up and we blog them.
February 4th, 2010 at 9:33 pm
[...] = this.parent.localToGlobal(pt); var col=mc.mcPoza.hitTestPoint(mouseX+pt.x,mouseY+pt.y ,true); localToGlobal() in AS3 not working « Blog « Orland Media Ltd This explains the method nicely. Best, [...]
March 4th, 2010 at 11:26 am
Please note this behaviour is similar to working with the SoundTransform object, which also requires you to write the result back into the object after altering, for example, the volume property.