localToGlobal()
in AS3 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 differences such as this are not mentioned by Adobe. But properly understood these methods are not complicated.
Thanks a lot for this post. π
You’re welcome!
Yes, that was easy after reading your tip. Especially
target.parent…
part helped a lot. Thanks!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!
π 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!
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. π
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.Thank you SO MUCH for this post.
Thanks, man! It helped much π
Thanks guys!Β This was really helpfull.
I just love you π
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?
Thank you!
brilliant!
Thanks
Thanks!!
Thanks man π
I love you mann
Bless ya.Β This had been driving me nuts.
Saved my Bacon – I owe you some bacon !
Β
Good information ! Thx
I’ve stumbled upon this article and i really don’t understand it !
localToGlobal works this way :
var corner:Point = new Point(0, 0);
var globalPt:point = myClip.localToGlobal(corner).
That’s it ! No need to call parents here !!
Amazing how after all these years, I learn the right way now πΒ
Author++Β