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

Adjusting DisplayObject colors<br />

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

You can use the methods of the ColorTransform class (flash.geom.ColorTransform) to adjust the color of a display<br />

object. Each display object has a transform property, which is an instance of the Transform class, and contains<br />

information about various transformations that are applied to the display object (such as rotation, changes in scale or<br />

position, and so forth). In addition to its information about geometric transformations, the Transform class also<br />

includes a colorTransform property, which is an instance of the ColorTransform class, and provides access to make<br />

color adjustm<strong>en</strong>ts to the display object. To access the color transformation information of a display object, you can use<br />

code such as this:<br />

var colorInfo:ColorTransform = myDisplayObject.transform.colorTransform;<br />

Once you’ve created a ColorTransform instance, you can read its property values to find out what color<br />

transformations have already be<strong>en</strong> applied, or you can set those values to make color changes to the display object. To<br />

update the display object after any changes, you must reassign the ColorTransform instance back to the<br />

transform.colorTransform property.<br />

var colorInfo:ColorTransform = myDisplayObject.transform.colorTransform;<br />

// Make some color transformations here.<br />

// Commit the change.<br />

myDisplayObject.transform.colorTransform = colorInfo;<br />

Setting color values with code<br />

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

The color property of the ColorTransform class can be used to assign a specific red, gre<strong>en</strong>, blue (RGB) color value to<br />

the display object. The following example uses the color property to change the color of the display object named<br />

square to blue, wh<strong>en</strong> the user clicks a button named blueBtn:<br />

// square is a display object on the Stage.<br />

// blueBtn, redBtn, gre<strong>en</strong>Btn, and blackBtn are buttons on the Stage.<br />

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

import flash.geom.ColorTransform;<br />

// Get access to the ColorTransform instance associated with square.<br />

var colorInfo:ColorTransform = square.transform.colorTransform;<br />

// This function is called wh<strong>en</strong> blueBtn is clicked.<br />

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

{<br />

// Set the color of the ColorTransform object.<br />

colorInfo.color = 0x003399;<br />

// apply the change to the display object<br />

square.transform.colorTransform = colorInfo;<br />

}<br />

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

Last updated 6/6/2012<br />

187

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

Saved successfully!

Ooh no, something went wrong!