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

// This function is called every time the mouse moves,<br />

// as long as the mouse button is pressed down.<br />

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

{<br />

// Move the dragged object to the location of the cursor, maintaining<br />

// the offset betwe<strong>en</strong> the cursor's location and the location<br />

// of the dragged object.<br />

draggedObject.x = ev<strong>en</strong>t.stageX - offsetX;<br />

draggedObject.y = ev<strong>en</strong>t.stageY - offsetY;<br />

}<br />

// Instruct Flash Player to refresh the scre<strong>en</strong> after this ev<strong>en</strong>t.<br />

ev<strong>en</strong>t.updateAfterEv<strong>en</strong>t();<br />

circle.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_DOWN, startDragging);<br />

circle.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_UP, stopDragging);<br />

square.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_DOWN, startDragging);<br />

square.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_UP, stopDragging);<br />

To ext<strong>en</strong>d this effect further, such as for a game where tok<strong>en</strong>s or cards are moved among piles, you could add the<br />

dragged object to the Stage’s display list wh<strong>en</strong> it’s “picked up,” and th<strong>en</strong> add it to another display list—such as the “pile”<br />

where it is dropped—wh<strong>en</strong> the mouse button is released.<br />

Finally, to <strong>en</strong>hance the effect, you could apply a drop shadow filter to the display object wh<strong>en</strong> it is clicked (wh<strong>en</strong> you<br />

start dragging it) and remove the drop shadow wh<strong>en</strong> the object is released. For details on using the drop shadow filter<br />

and other display object filters in ActionScript, see “Filtering display objects” on page 267.<br />

Panning and scrolling display objects<br />

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

If you have a display object that is too large for the area in which you want it to display it, you can use the scrollRect<br />

property to define the viewable area of the display object. In addition, by changing the scrollRect property in<br />

response to user input, you can cause the cont<strong>en</strong>t to pan left and right or scroll up and down.<br />

The scrollRect property is an instance of the Rectangle class, which is a class that combines the values needed to<br />

define a rectangular area as a single object. To initially define the viewable area of the display object, create a new<br />

Rectangle instance and assign it to the display object’s scrollRect property. Later, to scroll or pan, you read the<br />

scrollRect property into a separate Rectangle variable, and change the desired property (for instance, change the<br />

Rectangle instance’s x property to pan or y property to scroll). Th<strong>en</strong> you reassign that Rectangle instance to the<br />

scrollRect property to notify the display object of the changed value.<br />

For example, the following code defines the viewable area for a TextField object named bigText that is too tall to fit<br />

in the SWF file’s boundaries. Wh<strong>en</strong> the two buttons named up and down are clicked, they call functions that cause the<br />

cont<strong>en</strong>ts of the TextField object to scroll up or down by modifying the y property of the scrollRect Rectangle<br />

instance.<br />

Last updated 6/6/2012<br />

178

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

Saved successfully!

Ooh no, something went wrong!