Official explanation:
Except for textfield and video objects, the height of an object without content (for example, an empty Sprite) is 0, even if you tryHeight
Set it to another value.
If you setHeight
Attribute, thenScaley
The property will be adjusted accordingly (width and so on)
That is to say, an empty Sprite, even if you set the width and height, is useless (and if you set them manually, it will have a negative effect, as shown in the following figure.Code)
VaR _ sprite: SPRITE = new sprite (); _ Sprite. width = 100; _ Sprite. height = 100; addchild (_ sprite); trace (_ Sprite. width, _ Sprite. height); // 0 0_sprite.graphics.beginfill (0xff0000); _ Sprite. graphics. drawrect (0, 0, 400,300); _ Sprite. graphics. endfill (); trace (_ Sprite. width, _ Sprite. height); // The output is still 0
Note: The width and height of _ sprite are manually specified here, But 0 is still output when trace is used, and worse, even if a red rectangle of 400*300 is drawn in _ Sprite, the width and height of _ sprite are still 0, so that nothing can be seen on the stage!
If you remove the statement that sets the width and height manually:
VaR _ sprite: SPRITE = new sprite (); // _ Sprite. width = 100; // _ Sprite. height = 100; addchild (_ sprite); trace (_ Sprite. width, _ Sprite. height); // 0 0_sprite.graphics.beginfill (0xff0000); _ Sprite. graphics. drawrect (0, 0, 400,300); _ Sprite. graphics. endfill (); trace (_ Sprite. width, _ Sprite. height); // The output is 400 300
Now _ sprite will automatically adjust the size to 400*300 according to the display content, and the red rectangle is displayed normally.