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. If this post helped you please let us know.
(16 votes, average: 4.25 out of 5)
15 Responses to “localToGlobal() in AS3 not working”:
Yes, that was easy after reading your tip. Especially “target.parent…” part helped a lot. Thanks!
Brian 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!
Brian December 22nd, 2009 at 6:03 pm
problem solved
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!
[...] = 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, [...]
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.
This is by far the most visited post on our site – it’s great to see it helping so many people..
Mortimer July 2nd, 2010 at 1:44 pm
Common problem – simple solution, thankyoutnankyou!!
I do ask my self, though, why do I always have to trial-and-error such things for hours til Iget real frustrated while Adobe maintains online docs where info like yours could be inserted quite easily?
[...] it’s been pretty quiet about this blog for some time, for my new work place keeps me fairly busy. Not that this would be a bad thing. After wrapping my mind about concepts of code abstraction, collecting battle scars ( Hi Snepo ) , I now learn a great deal about everyday Flash and AS3.0 techniques, which I was happy to ignore for most of the time. One of those very straight forward things (LocalToGlobal and GlobalToLocal) really caused me headaches once and since I have never touched it again. This blog post has proven very helpful to me and obviously to many others. Here’s the link: http://www.orlandmedia.com/blog/actionscript-3/localtoglobal-in-as3-not-working/ [...]
15 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.
April 3rd, 2010 at 3:50 pm
Thank you SO MUCH for this post.
April 9th, 2010 at 8:36 am
Thanks, man! It helped much
April 10th, 2010 at 7:16 pm
Thanks guys! This was really helpfull.
April 26th, 2010 at 6:59 am
I just love you
April 26th, 2010 at 8:20 am
This is by far the most visited post on our site – it’s great to see it helping so many people..
July 2nd, 2010 at 1:44 pm
Common problem – simple solution, thankyoutnankyou!!
I do ask my self, though, why do I always have to trial-and-error such things for hours til Iget real frustrated while Adobe maintains online docs where info like yours could be inserted quite easily?
August 18th, 2010 at 6:20 am
[...] it’s been pretty quiet about this blog for some time, for my new work place keeps me fairly busy. Not that this would be a bad thing. After wrapping my mind about concepts of code abstraction, collecting battle scars ( Hi Snepo ) , I now learn a great deal about everyday Flash and AS3.0 techniques, which I was happy to ignore for most of the time. One of those very straight forward things (LocalToGlobal and GlobalToLocal) really caused me headaches once and since I have never touched it again. This blog post has proven very helpful to me and obviously to many others. Here’s the link: http://www.orlandmedia.com/blog/actionscript-3/localtoglobal-in-as3-not-working/ [...]