In the documentation for DisplayObject, it states that the width and height of the DisplayObject can not be changed if it is empty. Why is this restriction necessary? In every other framework I have used, you can resize containers that are empty. What is the reason for this madness?
views:
33answers:
2In Flash, a DisplayObject is not solely a container for other DisplayObjects, though it can be. In Flash, a DisplayObject is just that, a displayable object... one that can be added to the DisplayList. What would be the original size of an empty sprite? 0x0? 1x1? The size is therefore determined by the contents being displayed.
That being said, you can change the scaleX and scaleY properties of an empty DisplayObject.
var c:Sprite = new Sprite();
c.scaleX = c.scaleY = 2;
c.graphics.beginFill(0);
c.graphics.drawRect(0, 0, 200, 80);
c.graphics.endFill();
addChild(c);
The sprite is 400x160 on the stage. :)
The reason you can't change an empty object's width and height is that they are ultimately derived properties. Along with the object's content, scaleX
and scaleY
are what really determines the object's size - width
and height
are really just convenient ways to adjust the scale.
In other words, when you change an object's width
property, all Flash really does is to calculate what scaleX
would be necessary to achieve the desired width, and then change scaleX
to that value. When the object is empty, there is no scale value that would achieve any width except zero, so Flash ignores the request.