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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Display programming<br />

Controlling distortion wh<strong>en</strong> scaling<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Normally wh<strong>en</strong> a display object is scaled (for example, stretched horizontally), the resulting distortion is spread equally<br />

across the object, so that each part is stretched the same amount. For graphics and design elem<strong>en</strong>ts, this is probably<br />

what you want. However, sometimes it’s preferable to have control over which portions of the display object stretch<br />

and which portions remain unchanged. One common example of this is a button that’s a rectangle with rounded<br />

corners. With normal scaling, the corners of the button will stretch, making the corner radius change as the button<br />

resizes.<br />

However, in this case it would be preferable to have control over the scaling—to be able to designate certain areas<br />

which should scale (the straight sides and middle) and areas which shouldn’t (the corners)—so that scaling happ<strong>en</strong>s<br />

without visible distortion.<br />

You can use 9-slice scaling (Scale-9) to create display objects where you have control over how the objects scale. With<br />

9-slice scaling, the display object is divided into nine separate rectangles (a 3 by 3 grid, like the grid of a tic-tac-toe<br />

board). The rectangles ar<strong>en</strong>’t necessarily the same size—you designate where the grid lines are placed. Any cont<strong>en</strong>t that<br />

lies in the four corner rectangles (such as the rounded corners of a button) will not be stretched or compressed wh<strong>en</strong><br />

the display object scales. The top-c<strong>en</strong>ter and bottom-c<strong>en</strong>ter rectangles will scale horizontally but not vertically, while<br />

the left-middle and right-middle rectangles will scale vertically but not horizontally. The c<strong>en</strong>ter rectangle will scale<br />

both horizontally and vertically.<br />

Keeping this in mind, if you’re creating a display object and you want certain cont<strong>en</strong>t to never scale, you just have to<br />

make sure that the dividing lines of the 9-slice scaling grid are placed so that the cont<strong>en</strong>t <strong>en</strong>ds up in one of the corner<br />

rectangles.<br />

In ActionScript, setting a value for the scale9Grid property of a display object turns on 9-slice scaling for the object<br />

and defines the size of the rectangles in the object’s Scale-9 grid. You use an instance of the Rectangle class as the value<br />

for the scale9Grid property, as follows:<br />

myButton.scale9Grid = new Rectangle(32, 27, 71, 64);<br />

Last updated 6/6/2012<br />

181

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

Saved successfully!

Ooh no, something went wrong!