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

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

{<br />

myBall.alpha = .5;<br />

}<br />

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

You can also alter the transpar<strong>en</strong>cy of a display object using the color adjustm<strong>en</strong>ts available through the<br />

ColorTransform class. For more information, see “Adjusting DisplayObject colors” on page 187.<br />

Masking display objects<br />

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

You can use a display object as a mask to create a hole through which the cont<strong>en</strong>ts of another display object are visible.<br />

Defining a mask<br />

To indicate that a display object will be the mask for another display object, set the mask object as the mask property<br />

of the display object to be masked:<br />

// Make the object maskSprite be a mask for the object mySprite.<br />

mySprite.mask = maskSprite;<br />

The masked display object is revealed under all opaque (nontranspar<strong>en</strong>t) areas of the display object acting as the mask.<br />

For instance, the following code creates a Shape instance containing a red 100 by 100 pixel square and a Sprite instance<br />

containing a blue circle with a radius of 25 pixels. Wh<strong>en</strong> the circle is clicked, it is set as the mask for the square, so that<br />

the only part of the square that shows is the part that is covered by the solid part of the circle. In other words, only a<br />

red circle will be visible.<br />

// This code assumes it's being run within a display object container<br />

// such as a MovieClip or Sprite instance.<br />

import flash.display.Shape;<br />

// Draw a square and add it to the display list.<br />

var square:Shape = new Shape();<br />

square.graphics.lineStyle(1, 0x000000);<br />

square.graphics.beginFill(0xff0000);<br />

square.graphics.drawRect(0, 0, 100, 100);<br />

square.graphics.<strong>en</strong>dFill();<br />

this.addChild(square);<br />

// Draw a circle and add it to the display list.<br />

var circle:Sprite = new Sprite();<br />

circle.graphics.lineStyle(1, 0x000000);<br />

circle.graphics.beginFill(0x0000ff);<br />

circle.graphics.drawCircle(25, 25, 25);<br />

circle.graphics.<strong>en</strong>dFill();<br />

this.addChild(circle);<br />

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

{<br />

square.mask = circle;<br />

circle.removeEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, maskSquare);<br />

}<br />

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

Last updated 6/6/2012<br />

190

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

Saved successfully!

Ooh no, something went wrong!