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

The lineGradi<strong>en</strong>tStyle() method works similarly to beginGradi<strong>en</strong>tFill() except that in addition to defining the<br />

gradi<strong>en</strong>t, you must specify the thickness of the stroke using the lineStyle() method before drawing. The following<br />

code draws a box with a red, gre<strong>en</strong>, and blue gradi<strong>en</strong>t stroke:<br />

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

var gradi<strong>en</strong>tBoxMatrix:Matrix = new Matrix();<br />

gradi<strong>en</strong>tBoxMatrix.createGradi<strong>en</strong>tBox(200, 40, 0, 0, 0);<br />

myShape.graphics.lineStyle(5, 0);<br />

myShape.graphics.lineGradi<strong>en</strong>tStyle(Gradi<strong>en</strong>tType.LINEAR, [0xFF0000, 0x00FF00, 0x0000FF], [1,<br />

1, 1], [0, 128, 255], gradi<strong>en</strong>tBoxMatrix);<br />

myShape.graphics.drawRect(0, 0, 200, 40);<br />

this.addChild(myShape);<br />

For more information on the Matrix class, see “Using Matrix objects” on page 215.<br />

Using the Math class with drawing methods<br />

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

A Graphics object draws circles and squares, but can also draw more complex forms, particularly wh<strong>en</strong> the drawing<br />

methods are used in combination with the properties and methods of the Math class. The Math class contains<br />

constants of common mathematical interest, such as Math.PI (approximately 3.14159265...), a constant for the ratio<br />

of the circumfer<strong>en</strong>ce of a circle to its diameter. It also contains methods for trigonometry functions, including<br />

Math.sin(), Math.cos(), and Math.tan() among others. Drawing shapes using these methods and constants create<br />

more dynamic visual effects, particularly wh<strong>en</strong> used with repetition or recursion.<br />

Many methods of the Math class expect circular measurem<strong>en</strong>ts in units of radians rather than degrees. Converting<br />

betwe<strong>en</strong> these two types of units is a common use of the Math class:<br />

var degrees = 121;<br />

var radians = degrees * Math.PI / 180;<br />

trace(radians) // 2.111848394913139<br />

The following example creates a sine wave and a cosine wave, to highlight the differ<strong>en</strong>ce betwe<strong>en</strong> the Math.sin() and<br />

Math.cos() methods for a giv<strong>en</strong> value.<br />

var sinWavePosition = 100;<br />

var cosWavePosition = 200;<br />

var sinWaveColor:uint = 0xFF0000;<br />

var cosWaveColor:uint = 0x00FF00;<br />

var waveMultiplier:Number = 10;<br />

var waveStretcher:Number = 5;<br />

var i:uint;<br />

for(i = 1; i < stage.stageWidth; i++)<br />

{<br />

var sinPosY:Number = Math.sin(i / waveStretcher) * waveMultiplier;<br />

var cosPosY:Number = Math.cos(i / waveStretcher) * waveMultiplier;<br />

}<br />

graphics.beginFill(sinWaveColor);<br />

graphics.drawRect(i, sinWavePosition + sinPosY, 2, 2);<br />

graphics.beginFill(cosWaveColor);<br />

graphics.drawRect(i, cosWavePosition + cosPosY, 2, 2);<br />

Last updated 6/6/2012<br />

230

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

Saved successfully!

Ooh no, something went wrong!