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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Using the drawing API<br />

Drawing straight lines<br />

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

Wh<strong>en</strong> you call the lineTo() method, the Graphics object draws a straight line from the curr<strong>en</strong>t drawing point to the<br />

coordinates you specify as the two parameters in the method call, drawing with the line style you have specified. For<br />

example, this line of code puts the drawing point at the point 100, 100 th<strong>en</strong> draws a line to the point 200, 200:<br />

myShape.graphics.moveTo(100, 100);<br />

myShape.graphics.lineTo(200, 200);<br />

The following example draws red and gre<strong>en</strong> triangles with a height of 100 pixels:<br />

var triangleHeight:uint = 100;<br />

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

// red triangle, starting at point 0, 0<br />

triangle.graphics.beginFill(0xFF0000);<br />

triangle.graphics.moveTo(triangleHeight / 2, 0);<br />

triangle.graphics.lineTo(triangleHeight, triangleHeight);<br />

triangle.graphics.lineTo(0, triangleHeight);<br />

triangle.graphics.lineTo(triangleHeight / 2, 0);<br />

// gre<strong>en</strong> triangle, starting at point 200, 0<br />

triangle.graphics.beginFill(0x00FF00);<br />

triangle.graphics.moveTo(200 + triangleHeight / 2, 0);<br />

triangle.graphics.lineTo(200 + triangleHeight, triangleHeight);<br />

triangle.graphics.lineTo(200, triangleHeight);<br />

triangle.graphics.lineTo(200 + triangleHeight / 2, 0);<br />

this.addChild(triangle);<br />

Drawing curves<br />

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

The curveTo() method draws a quadratic Bézier curve. This draws an arc that connects two points (called anchor<br />

points) while b<strong>en</strong>ding toward a third point (called the control point). The Graphics object uses the curr<strong>en</strong>t drawing<br />

position as the first anchor point. Wh<strong>en</strong> you call the curveTo() method, you pass four parameters: the x and y<br />

coordinates of the control point, followed by the x and y coordinates of the second anchor point. For example, the<br />

following code draws a curve starting at point 100, 100 and <strong>en</strong>ding at point 200, 200. Because the control point is at<br />

point 175, 125, this creates a curve that moves to the right and th<strong>en</strong> downward:<br />

myShape.graphics.moveTo(100, 100);<br />

myShape.graphics.curveTo(175, 125, 200, 200);<br />

The following example draws red and gre<strong>en</strong> circular objects with a width and height of 100 pixels. Note that due to the<br />

nature of the quadratic Bézier equation, these are not perfect circles:<br />

Last updated 6/6/2012<br />

224

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

Saved successfully!

Ooh no, something went wrong!