在flex中,如何在使用VBox堆叠组件时获得cooridnnates? [英] In flex, how to get cooridnates when using VBox for stacking up components?
问题描述
如何在没有明确说明的情况下获得坐标。
可以将坐标相对于舞台转换。看看下面的代码: var box:VBox = new VBox;
var child:DisplayObject = new DisplayObject;
box.addChild(child);
child.addEventListener(FlexEvent.UPDATE_COMPLETE,updateCompleteHandler);
$ b私有函数updateCompleteHandler(fe:FlexEvent):void {
//找到全局坐标
var globalCoords:Point = child。 localToGlobal(new Point(0,0));
// coordsInBox就是你要找的
var coordsInBox:Point = box.globalToLocal(globalCoords);
$ / code>
关键是使用 localToGlobal
为子组件,然后 globalToLocal
来转换全局坐标,以便它们相对于盒子容器来表示。
请注意,在子对象调用 UPDATE_COMPLETE
之前,坐标不会被处理。
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 cooridnate 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);
}
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.
Please notice, that the coordinates won't be processed until UPDATE_COMPLETE
is called by the child object.
这篇关于在flex中,如何在使用VBox堆叠组件时获得cooridnnates?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!