A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: localToGlobal ?

  1. #1
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016

    localToGlobal ?

    Hi,

    I have a movieclip with a movieclip and am trying to find the _x and _y coordinated of it on the main stage.

    No matter what i try, it is returning the local coordinates.

    Am I just being stupid or is this mad ?
    Living the dream

  2. #2
    Senior Member siriuss's Avatar
    Join Date
    Jan 2003
    Location
    lost in thought
    Posts
    154
    From within MC:

    p = new Object()
    p.x = this._x
    p.y = this._y
    this.localToGlobal(p)
    trace("global x : " + p.x + " global y : " + p.y)

    Outside MC:
    p = new Object()
    p.x = _root.myMC.myOtherMC._x
    p.y = _root.myMC.myOtherMC._y
    _root.myMC.myOtherMC.localToGlobal(p)
    trace("global x : " + p.x + " global y : " + p.y)

    I think ? Always gets me too.
    ...?...

  3. #3
    A very senior man mrpauly99's Avatar
    Join Date
    Feb 2002
    Location
    Cool Britania
    Posts
    429
    i had this one recently. have to add the coords of the movie clips together to get the actual coords. it made sense at the time somehow.

  4. #4
    Senior Member siriuss's Avatar
    Join Date
    Jan 2003
    Location
    lost in thought
    Posts
    154
    Alternatively use this:

    code:

    MovieClip.prototype.L2G = function(p)
    {
    x = p.x
    y = p.y
    clip = this
    while(clip != _root)
    {
    x = x* (clip._xscale/100)
    y = y* (clip._yscale/100)
    x += clip._x
    y += clip._y
    clip = _parent
    }
    p.x = x
    p.y = y
    }



    is probably exactly the same as localToGlobal
    ...?...

  5. #5
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    siriuss, That wouldn't work if one or more of the movie clips were rotated.

  6. #6
    Senior Member siriuss's Avatar
    Join Date
    Jan 2003
    Location
    lost in thought
    Posts
    154
    doh!
    Cheers strille, forgot about that. lol
    ...?...

  7. #7
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    Originally posted by strille
    siriuss, That wouldn't work if one or more of the movie clips were rotated.
    thats the problem, I have this mc structure

    _root.arrow.pointer

    pointer is a blank mc on the tip of the arrow mc. I need to know the pos of pointer when the arrow is rotated.
    Living the dream

  8. #8
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    siriuss, it wouldn't surprise me if your function was faster than localToGlobal(). If that is the case it would still be good to have when you know the clips aren't rotated (which is most of the time, right?).

  9. #9
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    The code from siriuss with localToGlobal functions should work. Remember, its important where do you call localToGlobal from. _root.localToGlobal is different then _root.mc.localToGlobal.


    I used localToGlobal to calculate endpoint of robot arm in the first version of robot arm I made. Parts of hand where all mcs inside each other so they were easy to rotate. But then I abandoned that approach.
    Current version has all the mcs on the root and every time one of them is rotated, position and rotation of other mcs is calculated using simple math.

    http://www.tonypa.pri.ee/robootik_e.html

  10. #10
    Senior Member siriuss's Avatar
    Join Date
    Jan 2003
    Location
    lost in thought
    Posts
    154
    OK I've got this :

    code:

    MovieClip.prototype.L2G = function(p)
    {
    x = p.x
    y = p.y
    clip = this
    trace("clip : " + clip)
    while(clip != _root)
    {
    r = clip._rotation
    // transform rotation
    xr = x*Math.cos(r) - y*Math.sin(r)
    yr = x*Math.sin(r) + y*Math.cos(r)
    // transform scale
    x = xr* (clip._xscale/100)
    y = yr* (clip._yscale/100)
    // add to global pos
    x += clip._x
    y += clip._y
    clip = _parent
    }
    p.x = x
    p.y = y
    }



    But it doesn't look as if it's working correctly and my brain hurts. What order are you supposed to use for scale-rotation?
    ...?...

  11. #11
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    I've made some changes:
    code:
    MovieClip.prototype.L2G = function(p) {
    var x = p.x;
    var y = p.y;
    var clip = this;

    trace("clip : "+clip);

    while (clip != _root) {
    // transform scale
    var xs = x*(clip._xscale/100);
    var ys = y*(clip._yscale/100);
    // transform rotation
    var r = clip._rotation*(Math.PI/180);
    x = xs*Math.cos(r)-ys*Math.sin(r);
    y = xs*Math.sin(r)+ys*Math.cos(r);
    // add to global pos
    x += clip._x;
    y += clip._y;
    clip = clip._parent;
    }
    p.x = x;
    p.y = y;
    };

    You have to transform the scale first and then rotate x and y depending on that scale. And you had r in degrees when the cos and sin functions are in radians. And you had written clip = _parent, when it should be clip = clip._parent. I just tested it quickly and it seems to work. Now you just have to make it work with movie clips that are skewed.

  12. #12
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    I've just made a quick speed test and it seems localToGlobal() is about 10 times faster... Which is a bit of a surprise. Guess it's no point working on it further...

  13. #13
    Senior Member siriuss's Avatar
    Join Date
    Jan 2003
    Location
    lost in thought
    Posts
    154
    oops I frazzled my brain so much working out the rotation transformations I completely forgot about using radians.

    Cheers again strille, think I'll leave skewing to a more capable soul tho.

    S.
    ...?...

  14. #14
    Senior Member siriuss's Avatar
    Join Date
    Jan 2003
    Location
    lost in thought
    Posts
    154
    Originally posted by strille
    I've just made a quick speed test and it seems localToGlobal() is about 10 times faster... Which is a bit of a surprise. Guess it's no point working on it further...
    hmm strange. maybe it accesses lower level functions or flash stores positions as local and global?

    nevermind, nice exercise anyway.
    ...?...

  15. #15
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    This code just saved my butt. Had a problem with rotation and nested movieclips. I made a couple modifications cause I wasn't able to figure out how to use the prototype as it was.

    PHP Code:
    MovieClip.prototype.L2G = function() {
        var 
    temp = new Object();
        
        
    temp.newX 0;
        
    temp.newY 0;

        var 
    clip this;
        
        
    trace("clip : "+clip);

        while (
    clip != _root) {

            
    // transform scale
            
    var xs temp.newX*(clip._xscale/100);
            var 
    ys temp.newY*(clip._yscale/100);

            
    // transform rotation
            
    var clip._rotation*(Math.PI/180);
            
    temp.newX xs*Math.cos(r)-ys*Math.sin(r);
            
    temp.newY xs*Math.sin(r)+ys*Math.cos(r);

            
    // add to global pos
            
    temp.newX += clip._x;
            
    temp.newY += clip._y;

            
    clip clip._parent;

        }
        
        
    //trace("x : "+temp.newX+" , y : "+temp.newY);
        
        
    return temp;

    }; 
    Used it like "targetMovieClip.L2G().newX" to get the X position of a nested movieclip that was in a rotated object.

    Thanks to siriuss and strille.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center