的我有帶小孩(精靈)的容器(精靈)添加到它。 當我縮放容器,我想獲得孩子的新寬度,但是當我找到這些兒童的寬度,我總是得到原始尺寸,即使一切都得到視覺縮放,因爲它應該。 如何覆蓋孩子寬度?縮放的容器不影響孩子的寬度
更新: 我剛剛發現的children.graphics-「尺寸」可能造成的問題。當我離開圖形時,一切似乎都沒問題。是否有可能與兒童精靈同時縮放圖形?
的我有帶小孩(精靈)的容器(精靈)添加到它。 當我縮放容器,我想獲得孩子的新寬度,但是當我找到這些兒童的寬度,我總是得到原始尺寸,即使一切都得到視覺縮放,因爲它應該。 如何覆蓋孩子寬度?縮放的容器不影響孩子的寬度
更新: 我剛剛發現的children.graphics-「尺寸」可能造成的問題。當我離開圖形時,一切似乎都沒問題。是否有可能與兒童精靈同時縮放圖形?
您可以像下面使用DisplayObject.transform.pixelBounds.width
或DisplayObject.transform.pixelBounds.height
獲得子顯示對象的寬度和高度:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var squaresXml:XML =
<squares>
<square name="redSquare" color="0xFF0000" />
<square name="blueSquare" color="0x00FF00" />
<square name="greenSquare" color="0x0000FF" />
</squares>;
var size:Number = 100;
var squares:Sprite = new Sprite();
for (var i:uint = 0; i < squaresXml.children().length(); i++)
{
var square:Square = new Square(squaresXml.square[i][email protected], size);
square.name = squaresXml.square[i][email protected];
square.x = i * size;
squares.addChild(square);
}// end for
addChild(squares);
squares.scaleX = squares.scaleY = 2;
var redSquare:Square = squares.getChildByName("redSquare") as Square;
trace(redSquare.width); // output: 100
trace(redSquare.transform.pixelBounds.width); // output : 200;
}// end function
}// end class
}// end package
import flash.display.Shape;
import flash.display.Sprite;
internal class Square extends Shape
{
public function Square(color:uint, size:Number)
{
graphics.beginFill(color);
graphics.drawRect(0, 0, size, size);
graphics.endFill();
}// end function
}// end class
DisplayObject.transform.pixelBounds
基本上得到「一個Rectangle
對象,它定義顯示對象的邊界矩形舞臺」。
孩子寬度總是將是什麼,原來,滿刻度值。如果你想獲得它們的當前寬度,只需將該值與容器的縮放比例相乘即可。
因此,舉例來說,如果你的孩子寬度爲100個像素,並且您縮放容器的一半大小:100個像素×0.5 = 50個像素。或用代碼:actualChildWidth = container.scaleX * child.width;
我也建議使用您的顯示對象的變換concatenatedMatrix
。這將返回一個二維變換矩陣,乘以對象的父鏈到根級。
一旦得到生成的Matrix對象,您可以檢查其a
或d
屬性,以分別查看其根級x和y縮放因子。
是不是一次縮放所有值的可能性,因爲如果我確實將子寬設置爲您的actualChildWidth,那麼孩子會再次在視覺上縮放... – algro 2011-05-26 16:36:30
是的,您不應該設置此值,這是計算並用於任何你需要的兒童寬度。也許有點詳細說明你的問題,以便我們瞭解你想解決什麼問題! – dain 2011-05-26 20:03:14