13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Display programming<br />

// Resize a display object.<br />

square.width = 420;<br />

square.height = 420;<br />

// Determine the radius of a circle display object.<br />

var radius:Number = circle.width / 2;<br />

Changing the height or width of a display object causes the object to scale, meaning its cont<strong>en</strong>ts stretch or squeeze<br />

to fit in the new area. If the display object contains only vector shapes, those shapes will be redrawn at the new scale,<br />

with no loss in quality. Any bitmap graphic elem<strong>en</strong>ts in the display object will be scaled rather than redrawn. So, for<br />

example, a digital photo whose width and height are increased beyond the actual dim<strong>en</strong>sions of the pixel information<br />

in the image will be pixelated, making it look jagged.<br />

Wh<strong>en</strong> you change the width or height properties of a display object, Flash Player and AIR update the scaleX and<br />

scaleY properties of the object as well.<br />

Note: TextField objects are an exception to this scaling behavior. Text fields need to resize themselves to accommodate<br />

text wrapping and font sizes, so they reset their scaleX or scaleY values to 1 after resizing. However, if you adjust the scaleX<br />

or scaleY values of a TextField object, the width and height values change to accommodate the scaling values you provide.<br />

These properties repres<strong>en</strong>t the relative size of the display object compared to its original size. The scaleX and scaleY<br />

properties use fraction (decimal) values to repres<strong>en</strong>t perc<strong>en</strong>tage. For example, if a display object’s width has be<strong>en</strong><br />

changed so that it’s half as wide as its original size, the object’s scaleX property will have the value .5, meaning 50<br />

perc<strong>en</strong>t. If its height has be<strong>en</strong> doubled, its scaleY property will have the value 2, meaning 200 perc<strong>en</strong>t.<br />

// circle is a display object whose width and height are 150 pixels.<br />

// At original size, scaleX and scaleY are 1 (100%).<br />

trace(circle.scaleX); // output: 1<br />

trace(circle.scaleY); // output: 1<br />

// Wh<strong>en</strong> you change the width and height properties,<br />

// Flash Player changes the scaleX and scaleY properties accordingly.<br />

circle.width = 100;<br />

circle.height = 75;<br />

trace(circle.scaleX); // output: 0.6622516556291391<br />

trace(circle.scaleY); // output: 0.4966887417218543<br />

Size changes are not proportional. In other words, if you change the height of a square but not its width, its<br />

proportions will no longer be the same, and it will be a rectangle instead of a square. If you want to make relative<br />

changes to the size of a display object, you can set the values of the scaleX and scaleY properties to resize the object,<br />

as an alternative to setting the width or height properties. For example, this code changes the width of the display<br />

object named square, and th<strong>en</strong> alters the vertical scale (scaleY) to match the horizontal scale, so that the size of the<br />

square stays proportional.<br />

// Change the width directly.<br />

square.width = 150;<br />

// Change the vertical scale to match the horizontal scale,<br />

// to keep the size proportional.<br />

square.scaleY = square.scaleX;<br />

Last updated 6/6/2012<br />

180

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!