Flash as3 understanding localToGlobal
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.
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?
精彩评论