开发者

Is there a reason I can't change the width and height of an empty display object?

开发者 https://www.devze.com 2023-01-11 09:49 出处:网络
In 开发者_JAVA技巧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 fra

In 开发者_JAVA技巧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?


In 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消