在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