11

I'm just having a little bit of trouble understanding Flash's localToGlobal functionality. I have a movieClip, which is nested inside a whole bunch of other movieclips. When that nested clip is clicked, I want to find its position, and moved the topmost containing clip by an amount such that the nested clip is in the center of the stage (basically what I have is a tree diagram, and the effect I want is that the treeContainer pans to the clicked "branch" as the center of the stage)

So I have this:

var treePoint = new Point (treeContainer.x,treeContainer.y); //since treePoint's parent is the stage, don't need global here.

var groupPoint = new Point (groupClip.x,groupClip.y);
var groupPointGlobal = groupClip.localToGlobal(groupPoint);
var stageCenter = new Point (int(stage.stageWidth/2),int(stage.stageHeight)/2);

var shiftAmount = ???

Thanks for any help you can provide.

mheavers
  • 29,530
  • 58
  • 194
  • 315

1 Answers1

23

A clip's x,y location is always relative to it's parent. So unless it's a child of the stage, or the parent is at 0,0 - you can use localToGlobal to give you it's location on the stage.

var globalPoint:Point = myClip.localToGlobal(new Point(0,0));

That would give you the global position of that clip.

But from the sounds of it, you want to go the other way and do globalToLocal right ?

globalToLocal will return a local position , based on a global location.

So if you wanted to set a clip's local position so that it will be positioned at center of the screen -- lets assume it's 320,240.

var localPoint:Point = myClip.parent.globalToLocal(new Point(320,240));
myClip.x = localPoint.x;
myClip.y = localPoint.y;

We use the parent, because thats what the clip will be be relative to.

make sense?

prototypical
  • 6,731
  • 3
  • 24
  • 34
  • 1
    Ah - I see what I was doing wrong - I DO want to go localToGlobal, but I needed to be setting the globalPoint via your method above. I was trying to set globalToLocal using the coordinates of the groupClip, not via the 0,0 position of the stage. – mheavers May 19 '11 at 20:40
  • cool, I wasn't sure exactly what you were trying to do, but I figured an example of each would help either way. – prototypical May 19 '11 at 20:55