在flex中,如何在使用VBox堆叠组件时获得cooridnnates? [英] In flex, how to get cooridnates when using VBox for stacking up components?

查看:180
本文介绍了在flex中,如何在使用VBox堆叠组件时获得cooridnnates?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在flex中,我使用VBox& HBox堆栈组件。当我尝试获取组件的x,y坐标时,我总是得到0.如果我指定了像mx:VBox x =120的cooridnate,那么我就得到了这个值。



如何在没有明确说明的情况下获得坐标。

可以将坐标相对于舞台转换。看看下面的代码:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆