在Flex中,使用VBox堆叠组件时如何获取坐标? [英] In flex, how to get coordinates when using VBox for stacking up components?
问题描述
在flex中,我正在使用VBox& HBox用来堆叠组件.当我尝试获取组件的x,y坐标时,我总是得到0.如果我像mx:VBox x ="120"这样指定坐标,那么我会得到值.
In flex, I am using VBox & HBox to stack components. When I try to get x,y coordinate of a component, I always get 0. If I specify coordinate like mx:VBox x="120", then I get the value.
如何在没有明确说明的情况下获取坐标.
How can I get the coordinate without explicitly stating it.
推荐答案
您可以相对于舞台平移坐标.查看下面的代码:
You can translate the coordinates relatively to the stage. Check out the code below:
var box:VBox = new VBox;
var child:DisplayObject = new DisplayObject;
box.addChild(child);
child.addEventListener(FlexEvent.UPDATE_COMPLETE, updateCompleteHandler);
...
private function updateCompleteHandler(fe:FlexEvent):void{
// find global coordinates
var globalCoords:Point = child.localToGlobal(new Point(0,0));
// coordsInBox is what you are looking for
var coordsInBox:Point = box.globalToLocal(globalCoords);
}
关键是要使用localToGlobal
作为子组件,然后使用globalToLocal
转换全局坐标,以便相对于Box容器表达它们.
The point is to use localToGlobal
for the child components and then globalToLocal
to translate the global coordinates so that they were expressed relatively to the box container.
请注意,在子对象调用UPDATE_COMPLETE
之前,不会处理坐标.
Please notice, that the coordinates won't be processed until UPDATE_COMPLETE
is called by the child object.
这篇关于在Flex中,使用VBox堆叠组件时如何获取坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!