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

Manipulating display objects<br />

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

Regardless of which display object you choose to use, there are a number of manipulations that all display objects have<br />

in common as elem<strong>en</strong>ts that are displayed on the scre<strong>en</strong>. For example, they can all be positioned on the scre<strong>en</strong>, moved<br />

forward or backward in the stacking order of display objects, scaled, rotated, and so forth. Because all display objects<br />

inherit this functionality from their common base class (DisplayObject), this functionality behaves the same whether<br />

you’re manipulating a TextField instance, a Video instance, a Shape instance, or any other display object. The<br />

following sections detail several of these common display object manipulations.<br />

Changing position<br />

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

The most basic manipulation to any display object is positioning it on the scre<strong>en</strong>. To set a display object’s position,<br />

change the object’s x and y properties.<br />

myShape.x = 17;<br />

myShape.y = 212;<br />

The display object positioning system treats the Stage as a Cartesian coordinate system (the common grid system with<br />

a horizontal x axis and vertical y axis). The origin of the coordinate system (the 0,0 coordinate where the x and y axes<br />

meet) is at the top-left corner of the Stage. From there, x values are positive going right and negative going left, while<br />

(in contrast to typical graphing systems) y values are positive going down and negative going up. For example, the<br />

previous lines of code move the object myShape to the x coordinate 17 (17 pixels to the right of the origin) and y<br />

coordinate 212 (212 pixels below the origin).<br />

By default, wh<strong>en</strong> a display object is created using ActionScript, the x and y properties are both set to 0, placing the<br />

object at the top-left corner of its par<strong>en</strong>t cont<strong>en</strong>t.<br />

Changing position relative to the Stage<br />

It’s important to remember that the x and y properties always refer to the position of the display object relative to the<br />

0,0 coordinate of its par<strong>en</strong>t display object’s axes. So for a Shape instance (such as a circle) contained inside a Sprite<br />

instance, setting the Shape object’s x and y properties to 0 will place the circle at the top-left corner of the Sprite, which<br />

is not necessarily the top-left corner of the Stage. To position an object relative to the global Stage coordinates, you can<br />

use the globalToLocal() method of any display object to convert coordinates from global (Stage) coordinates to local<br />

(display object container) coordinates, like this:<br />

Last updated 6/6/2012<br />

173

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

Saved successfully!

Ooh no, something went wrong!