Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

actionscript 3 - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...