2015-06-11 40 views
0

這應該很簡單。我錯過了什麼? 創建一個雪碧(容器),把它放在顯示列表上,添加一個新的雪碧(矩形)容器。 改變孩子的身高(rect)。 雖然正確渲染,但父級(容器)和子級(rect)的高度值未正確報告。 感謝您的協助。AS3顯示物體高度未正確報告

var container:Sprite = new Sprite(); 
addChild(container); 

[Embed(source = "../lib/rectangle.swf")] // height is 100 
var Rect:Class; 
var rect:Sprite = new Rect(); 

trace(rect.height); // 100 is correct before placement in container 
container.addChild(rect); 
trace(rect.height); // 100 is correct after placement in container 
trace(container.height); // 0 is not correct; should be 100 

rect.height = rect.height + 100; // renders correctly at new height 
trace(rect.height); // 100 is not correct; should be 200 
trace(container.height); // 0 is not correct; should be 200 
+0

你說精靈(容器)內的精靈(矩形),但你的例子顯示精靈內的SWF。 SWF中的內容是什麼?它是壓倒大小的屬性? – Aaron

+0

SWF是填充矩形的矢量圖。它的高度是100px,寬度是4px。我不知道嵌入的SWF對sprite屬性有什麼影響。如果精靈的屬性在AS3中更改,精靈將正常呈現。 – Gus

+0

嵌入式swf嵌入爲二進制文件,因此其行爲有所不同。如果你創建了一個克隆,那麼這個克隆就會正常工作。 – BotMaster

回答

0

顯然你的container不在舞臺顯示列表中。如果測量的DisplayObject不在顯示列表上(任何地方),則其尺寸屬性在自己的顯示列表發生變化時不能正確重新計算。這顯然是爲了節省Flash引擎處理構建複雜的框架或任何其他複雜容器的時間,並且只有在容器放置到舞臺時才進行重新計算。解決方法是將要測量的對象放置在舞臺上(無論如何),收集其屬性,然後將其從舞臺上移除。一個例子:

trace(this.stage); // [object Stage] - to make sure we can access stage in here 
var sp:Sprite=new Sprite(); 
var b:Bitmap=new Bitmap(new BitmapData(100,100)); 
trace(sp.width); // should return 0 
trace(sp.height); // 0 also 
sp.addChild(b); 
trace(sp.height); // 0 again 
trace(b.height); // should return 100, as the bitmap data is specified 
// as well as in your case, the class's width and height are precalculated 
addChild(sp); 
trace(sp.height); // returns 100, as expected 
removeChild(sp); 
trace(sp.height); // 100, stored 
sp.removeChild(b); 
trace(sp.height); // should also return 100, while there's no content in the sprite 

也有另一種可能性,一個對象關階段的width可以是in the negative,將溶液再次是相同的 - 臺上放對象,得到寬度,從階段除去對象。