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 />

import flash.ev<strong>en</strong>ts.MouseEv<strong>en</strong>t;<br />

import flash.geom.Rectangle;<br />

// Define the initial viewable area of the TextField instance:<br />

// left: 0, top: 0, width: TextField's width, height: 350 pixels.<br />

bigText.scrollRect = new Rectangle(0, 0, bigText.width, 350);<br />

// Cache the TextField as a bitmap to improve performance.<br />

bigText.cacheAsBitmap = true;<br />

// called wh<strong>en</strong> the "up" button is clicked<br />

function scrollUp(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

// Get access to the curr<strong>en</strong>t scroll rectangle.<br />

var rect:Rectangle = bigText.scrollRect;<br />

// Decrease the y value of the rectangle by 20, effectively<br />

// shifting the rectangle down by 20 pixels.<br />

rect.y -= 20;<br />

// Reassign the rectangle to the TextField to "apply" the change.<br />

bigText.scrollRect = rect;<br />

}<br />

// called wh<strong>en</strong> the "down" button is clicked<br />

function scrollDown(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

// Get access to the curr<strong>en</strong>t scroll rectangle.<br />

var rect:Rectangle = bigText.scrollRect;<br />

// Increase the y value of the rectangle by 20, effectively<br />

// shifting the rectangle up by 20 pixels.<br />

rect.y += 20;<br />

// Reassign the rectangle to the TextField to "apply" the change.<br />

bigText.scrollRect = rect;<br />

}<br />

up.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, scrollUp);<br />

down.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, scrollDown);<br />

As this example illustrates, wh<strong>en</strong> you work with the scrollRect property of a display object, it’s best to specify that<br />

Flash Player or AIR should cache the display object’s cont<strong>en</strong>t as a bitmap, using the cacheAsBitmap property. Wh<strong>en</strong><br />

you do so, Flash Player and AIR don’t have to re-draw the <strong>en</strong>tire cont<strong>en</strong>ts of the display object each time it is scrolled,<br />

and can instead use the cached bitmap to r<strong>en</strong>der the necessary portion directly to the scre<strong>en</strong>. For details, see “Caching<br />

display objects” on page 182.<br />

Manipulating size and scaling objects<br />

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

You can measure and manipulate the size of a display object in two ways, using either the dim<strong>en</strong>sion properties (width<br />

and height) or the scale properties (scaleX and scaleY).<br />

Every display object has a width property and a height property, which are initially set to the size of the object in<br />

pixels. You can read the values of those properties to measure the size of the display object. You can also specify new<br />

values to change the size of the object, as follows:<br />

Last updated 6/6/2012<br />

179

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

Saved successfully!

Ooh no, something went wrong!