2015-06-11 31 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,将溶液再次是相同的 - 台上放对象,得到宽度,从阶段除去对象。