As3: when using sprite as a container, it is best not to specify width, height

Source: Internet
Author: User

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 tryHeightSet it to another value.

If you setHeightAttribute, thenScaleyThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.