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

import flash.display.Loader;<br />

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

import flash.geom.Transform;<br />

import flash.geom.ColorTransform;<br />

import flash.net.URLRequest;<br />

// Load an image onto the Stage.<br />

var loader:Loader = new Loader();<br />

var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image1.jpg");<br />

loader.load(url);<br />

this.addChild(loader);<br />

// This function is called wh<strong>en</strong> the mouse moves over the loaded image.<br />

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

{<br />

// Access the ColorTransform object for the Loader (containing the image)<br />

var colorTransformer:ColorTransform = loader.transform.colorTransform;<br />

}<br />

// Set the red and gre<strong>en</strong> multipliers according to the mouse position.<br />

// The red value ranges from 0% (no red) wh<strong>en</strong> the cursor is at the left<br />

// to 100% red (normal image appearance) wh<strong>en</strong> the cursor is at the right.<br />

// The same applies to the gre<strong>en</strong> channel, except it's controlled by the<br />

// position of the mouse in the y axis.<br />

colorTransformer.redMultiplier = (loader.mouseX / loader.width) * 1;<br />

colorTransformer.gre<strong>en</strong>Multiplier = (loader.mouseY / loader.height) * 1;<br />

// Apply the changes to the display object.<br />

loader.transform.colorTransform = colorTransformer;<br />

loader.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_MOVE, adjustColor);<br />

Rotating objects<br />

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

Display objects can be rotated using the rotation property. You can read this value to find out whether an object has<br />

be<strong>en</strong> rotated, or to rotate the object you can set this property to a number (in degrees) repres<strong>en</strong>ting the amount of<br />

rotation to be applied to the object. For instance, this line of code rotates the object named square 45 degrees (one<br />

eighth of one complete revolution):<br />

square.rotation = 45;<br />

Alternatively, you can rotate a display object using a transformation matrix, described in “Working with geometry” on<br />

page 209.<br />

Fading objects<br />

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

You can control the transpar<strong>en</strong>cy of a display object to make it partially (or completely transpar<strong>en</strong>t), or change the<br />

transpar<strong>en</strong>cy to make the object appear to fade in or out. The DisplayObject class’s alpha property defines the<br />

transpar<strong>en</strong>cy (or more accurately, the opacity) of a display object. The alpha property can be set to any value betwe<strong>en</strong><br />

0 and 1, where 0 is completely transpar<strong>en</strong>t, and 1 is completely opaque. For example, these lines of code make the<br />

object named myBall partially (50 perc<strong>en</strong>t) transpar<strong>en</strong>t wh<strong>en</strong> it is clicked with the mouse:<br />

Last updated 6/6/2012<br />

189

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

Saved successfully!

Ooh no, something went wrong!