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

Using the drawing API<br />

Animating with the drawing API<br />

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

One advantage of creating cont<strong>en</strong>t with the drawing API is that you are not limited to positioning your cont<strong>en</strong>t once.<br />

What you draw can be modified by maintaining and modifying the variables you use to draw. You can convey<br />

animation by changing variables and redrawing, either over a period of frames or with a timer.<br />

For example, the following code changes the display with each passing frame (by list<strong>en</strong>ing to the Ev<strong>en</strong>t.ENTER_FRAME<br />

ev<strong>en</strong>t), increm<strong>en</strong>ting the curr<strong>en</strong>t degree count, and directs the graphics object to clear and redraw with the updated<br />

position.<br />

stage.frameRate = 31;<br />

var curr<strong>en</strong>tDegrees:Number = 0;<br />

var radius:Number = 40;<br />

var satelliteRadius:Number = 6;<br />

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

container.x = stage.stageWidth / 2;<br />

container.y = stage.stageHeight / 2;<br />

addChild(container);<br />

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

container.addChild(satellite);<br />

addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.ENTER_FRAME, doEveryFrame);<br />

function doEveryFrame(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

curr<strong>en</strong>tDegrees += 4;<br />

var radians:Number = getRadians(curr<strong>en</strong>tDegrees);<br />

var posX:Number = Math.sin(radians) * radius;<br />

var posY:Number = Math.cos(radians) * radius;<br />

satellite.graphics.clear();<br />

satellite.graphics.beginFill(0);<br />

satellite.graphics.drawCircle(posX, posY, satelliteRadius);<br />

}<br />

function getRadians(degrees:Number):Number<br />

{<br />

return degrees * Math.PI / 180;<br />

}<br />

To produce a significantly differ<strong>en</strong>t result, you can experim<strong>en</strong>t by modifying the initial seed variables at the beginning<br />

of the code, curr<strong>en</strong>tDegrees, radius, and satelliteRadius. For example, try shrinking the radius variable and/or<br />

increasing the totalSatellites variable. This is only one example of how the drawing API can create a visual display<br />

whose complexity conceals the simplicity of its creation.<br />

Last updated 6/6/2012<br />

231

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

Saved successfully!

Ooh no, something went wrong!