How to use localToGlobal() in ActionScript 3.0

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.

Cheap web hosting

Comments on this post

  1. James says:

    Thanks a lot for this post. πŸ™‚

  2. sw says:

    Yes, that was easy after reading your tip. Especially target.parent… part helped a lot. Thanks!

  3. Brian says:

    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 says:

      πŸ™‚ 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!

      • Orland Media says:

        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. πŸ™‚

  4. Orland Media says:

    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.

  5. Matthew says:

    Thank you SO MUCH for this post.

  6. Yuriy says:

    Thanks, man! It helped much πŸ™‚

  7. ukasz says:

    Thanks guys!Β  This was really helpfull.

  8. Niallith says:

    I just love you πŸ™‚

  9. Mortimer says:

    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?

  10. Chris says:

    Thank you!

  11. sb says:

    brilliant!
    Thanks

  12. fp says:

    Thanks!!

  13. Thanks man πŸ™‚

  14. mutlut says:

    I love you mann

  15. bhnh says:

    Bless ya.Β  This had been driving me nuts.

  16. King Baggot says:

    Saved my Bacon – I owe you some bacon !
    Β 

  17. Good information ! Thx

  18. Uli says:

    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 !!

  19. Adrian says:

    Amazing how after all these years, I learn the right way now πŸ˜€Β 

    Author++Β 

Add a comment:

*