在flex中,我使用VBox & HBox堆栈组件。当我尝试获取组件的x,y坐标时,我总是得到0.如果我指定像mx:VBox x =“120”这样的cooridnate,那么我会得到该值。在flex中,如何在使用VBox堆叠组件时获得cooridnnates?
我怎样才能没有明确说明它的坐标。
在flex中,我使用VBox & HBox堆栈组件。当我尝试获取组件的x,y坐标时,我总是得到0.如果我指定像mx:VBox x =“120”这样的cooridnate,那么我会得到该值。在flex中,如何在使用VBox堆叠组件时获得cooridnnates?
我怎样才能没有明确说明它的坐标。
组件的X和Y值总是相对于组件的父。 directionsHelp.x
和directionsHelp.y
都将返回相对于包含它们的VBox的位置,除非您明确设置值或在其周围插入其他组件,否则将默认为0。
的是要记住的localToGlobal()
的是,你必须从孩子调用它。如果你有一个Application
,你只需要调用localToGlobal(new Point(child.x, child.y))
,它会尝试应用相对于内给定点重返舞台(因为你没有指定什么“本地”的),因此这将不进行转换,它会因此保持等于(0,0)。
如果您调用child.localToGlobal(new Point(child.x, child.y))
,它会返回孩子相对于舞台的位置值,无论孩子在舞台上偏移了多少,它都会转换给定点。
这里有一个快速的应用程序来演示:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF">
<mx:Script>
<![CDATA[
private function updateCoords():void
{
var point:Point = new Point(directionsHelp.x, directionsHelp.y);
point = directionsHelp.localToGlobal(point);
directionsHelp.text = "My stage coordinates are ("+point.x+", "+point.y+")";
}
]]>
</mx:Script>
<mx:VBox>
<mx:Box height="100" width="100" borderStyle="solid" borderColor="#000000"
horizontalAlign="center" verticalAlign="middle">
<mx:Label text="100 x 100" />
</mx:Box>
<mx:VBox>
<mx:Text id="directionsHelp" color="#4FA4CD" fontSize="8" fontWeight="bold"
text="Click the button to display my position on the stage." />
<mx:Button label="Get Position" click="updateCoords()" />
</mx:VBox>
</mx:VBox>
</mx:Application>
如果您在VBox中有组件,则该组件的x和y值将被忽略;并且很可能会被VBox布局逻辑重置。 – JeffryHouser 2010-08-10 00:39:40
不是真正的afaik。加上'+ “当地COORDS是( ”+ directionsHelp.x +“, ”+ directionsHelp.y +“)”;'我'directionsHelp.text'分配上面,然后复制'directionsHelp'文本控件上方的100×100箱,输出将是: '我的舞台坐标是(0,318),本地(0,106)' 因此,Text控件的局部X和Y坐标已由VBox布局逻辑更新。 – orlade 2010-08-10 00:53:52
可以相对平移坐标舞台。看看下面的代码:
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
到全局坐标转换,使它们相对表示到箱集装箱。
请注意,该坐标将不会被处理,直到UPDATE_COMPLETE
由子对象调用。
我不明白你想要做什么或者得到什么结果。你能分享一些代码吗? – JeffryHouser 2010-08-09 21:21:55
MX:的AddChild对于relativeTo = “{mainBox}” \t \t \t \t MX:垂直框ID = “BOX1” 的verticalGap = “0” 宽度= “100%” \t \t \t \t \t \t \t \t \t \t \t \t MX:的htmlText \t \t \t MX:文本> \t \t \t \t MX:VBox中 –
svirk
2010-08-09 21:32:42
现在,如果我尝试获取的y坐标directonsHelp,我得到0.我甚至尝试过localToGlobal方法,但结果也是0。我猜想它可能与组件堆叠的方式有关。问题是:为什么我得到价值0而不是正确的y坐标? – svirk 2010-08-09 21:34:25