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

In addition, the GraphicsPath class has its own GraphicsPath.moveTo(), GraphicsPath.lineTo(),<br />

GraphicsPath.curveTo(), GraphicsPath.wideLineTo(), and GraphicsPath.wideMoveTo() utility methods to<br />

easily define those commands for a GraphicsPath instance. These utility methods ease defining or updating the<br />

commands and data values directly.<br />

Once you have a collection of IGraphicsData instances, use Graphics.drawGraphicsData() method to r<strong>en</strong>der the<br />

graphics. The Graphics.drawGraphicsData() method runs a vector of IGraphicsData instances through the<br />

drawing API in sequ<strong>en</strong>tial order:<br />

// stroke object<br />

var stroke:GraphicsStroke = new GraphicsStroke(3);<br />

stroke.joints = JointStyle.MITER;<br />

stroke.fill = new GraphicsSolidFill(0x102020);// solid stroke<br />

// fill object<br />

var fill:GraphicsGradi<strong>en</strong>tFill = new GraphicsGradi<strong>en</strong>tFill();<br />

fill.colors = [0x0000FF, 0xEEFFEE];<br />

fill.matrix = new Matrix();<br />

fill.matrix.createGradi<strong>en</strong>tBox(70,70, Math.PI/2);<br />

// path object<br />

var path:GraphicsPath = new GraphicsPath(new Vector.(), new Vector.());<br />

path.commands.push(1,2,2);<br />

path.data.push(125,0, 50,100, 175,0);<br />

// combine objects for complete drawing<br />

var drawing:Vector. = new Vector.();<br />

drawing.push(stroke, fill, path);<br />

// draw the drawing<br />

graphics.drawGraphicsData(drawing);<br />

By modifying one value in the path used by the drawing in the example, the shape can be redrawn multiple times for<br />

a more complex image:<br />

// draw the drawing multiple times<br />

// change one value to modify each variation<br />

graphics.drawGraphicsData(drawing);<br />

path.data[2] += 200;<br />

graphics.drawGraphicsData(drawing);<br />

path.data[2] -= 150;<br />

graphics.drawGraphicsData(drawing);<br />

path.data[2] += 100;<br />

graphics.drawGraphicsData(drawing);<br />

path.data[2] -= 50;graphicsS.drawGraphicsData(drawing);<br />

Though IGraphicsData objects can define fill and stroke styles, the fill and stroke styles are not a requirem<strong>en</strong>t. In other<br />

words, Graphics class methods can be used to set styles while IGraphicsData objects can be used to draw a saved<br />

collection of paths, or vice-versa.<br />

Note: Use the Graphics.clear() method to clear out a previous drawing before starting a new one; unless you're adding<br />

on to the original drawing, as se<strong>en</strong> in the example above. As you change a single portion of a path or collection of<br />

IGraphicsData objects, redraw the <strong>en</strong>tire drawing to see the changes.<br />

Wh<strong>en</strong> using graphics data classes, the fill is r<strong>en</strong>dered wh<strong>en</strong>ever three or more points are drawn, because the shape is<br />

inher<strong>en</strong>tly closed at that point. Ev<strong>en</strong> though the fill closes, the stroke does not, and this behavior is differ<strong>en</strong>t than wh<strong>en</strong><br />

using multiple Graphics.lineTo() or Graphics.moveTo() commands.<br />

Last updated 6/6/2012<br />

240

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

Saved successfully!

Ooh no, something went wrong!