Skip to main content
Known Participant
October 16, 2010
Answered

localToGlobal reporting wrong coordinates

  • October 16, 2010
  • 1 reply
  • 1488 views

At least, I think it is.

Say I have a movieclip called theScreen a 0,0. It is 800 wide, 700 high

theScreen contains a movieclip called theStatusArea at 600,0. It is 200 wide by 700 high

theStausArea contains a movieClip called pieces at 10, 410. It is 180 wide by 180 high

pieces contains a number of movieclips (triangle, square, etc.),

    all of these sub movieclips are more narrow than the pieces container and are all set added to the pieces container at 0,0

var pt:Point = new Point(triangle.x, triangle.y)

pt= triangle.localToGlobal(pt);

trace(pt); // should be 610, 410 at least at the start.

When I drag the triangle, it looks like it's doing it's reporting it right until I get halfway across the screen, at which point the x numbers all report as negative. I'm not sure what's going on, but it seems that I'm missing something here.

This topic has been closed for replies.
Correct answer EnochTwig

So this is weird, and I don't know why this works, but when I replace this line

    pt1=obj.localToGlobal(pt1);

with this

    pt1=obj.parent.localToGlobal(pt1);

everything gets reported correctly. I still have no idea why I can't convert the point directly on the object and I don't think I'm doing anything weird.
Thanks for your help!

CharlesTCR wrote:

So this is weird, and I don't know why this works, but when I replace this line

    pt1=obj.localToGlobal(pt1);

with this

    pt1=obj.parent.localToGlobal(pt1);

everything gets reported correctly. I still have no idea why I can't convert the point directly on the object and I don't think I'm doing anything weird.
Thanks for your help!

I've run into this problem myself a few times... the syntax of localToGlobal can be a bit decieving.

This code gets the object's coordinates within its parent's coordinate system and makes a new point from them:

pt1 = new Point(obj.x, obj.y);

This code gets the global coordinates of obj's coordinates (pt1) within its own (not its parent's!) coordinate system:

pt1 = obj.localToGlobal(pt1);

So basically you are getting a point in one coordinate system and then finding that point's global coordinates in it's child's system.

So your second line of code is correct, you find a point in the parent, and then find the global coordinates of that point within the parent.

1 reply

kglad
Community Expert
Community Expert
October 16, 2010

you're probably failing to reset pt to (0,0).

Known Participant
October 18, 2010

I'm not sure what you mean by resetting it since the pt variable is local to this one function  I'm pasting the code below. It's triggered just by clicking on the movieclip.

public function polySelect(event:MouseEvent):void {

var obj=event.currentTarget;

polyPick=event.currentTarget.name;

  for (var i:uint=0; i< playPolySides.length; i++) {

    for (var j:uint=0; j<playPolySides.length; j++) {

      if (i==polyPick) {

        playPolySides.visible=true;

      } else {

        playPolySides.visible=false;

      }

    }

  }

  // we're faking the registration point of object so it can rotate around its center

  // so we have to subtract half the height and width

  var pt1:Point= new Point(obj.x - (obj.width/2), obj.y - (obj.height/2));

  pt1=obj.localToGlobal(pt1);

  trace(polyPick, pt1, obj.x, obj.y);

}

kglad
Community Expert
Community Expert
October 18, 2010

i can't tell what you're doing with that code snippet.

i can tell you localToGlobal() works because i've not had a problem with it and i've not seen others posting issues here.