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

import flash.display.Shape;<br />

import flash.display.Gradi<strong>en</strong>tType;<br />

import flash.geom.Matrix;<br />

var type:String = Gradi<strong>en</strong>tType.RADIAL;<br />

var colors:Array = [0x00FF00, 0x000088];<br />

var alphas:Array = [1, 1];<br />

var ratios:Array = [0, 255];<br />

var spreadMethod:String = SpreadMethod.PAD;<br />

var interp:String = InterpolationMethod.LINEAR_RGB;<br />

var focalPtRatio:Number = 0;<br />

var matrix:Matrix = new Matrix();<br />

var boxWidth:Number = 50;<br />

var boxHeight:Number = 100;<br />

var boxRotation:Number = Math.PI/2; // 90°<br />

var tx:Number = 25;<br />

var ty:Number = 0;<br />

matrix.createGradi<strong>en</strong>tBox(boxWidth, boxHeight, boxRotation, tx, ty);<br />

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

square.graphics.beginGradi<strong>en</strong>tFill(type,<br />

colors,<br />

alphas,<br />

ratios,<br />

matrix,<br />

spreadMethod,<br />

interp,<br />

focalPtRatio);<br />

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

addChild(square);<br />

Note that the width and height of the gradi<strong>en</strong>t fill is determined by the width and height of the gradi<strong>en</strong>t matrix rather<br />

than the width or height that is drawn using the Graphics object. Wh<strong>en</strong> drawing with the Graphics object, you draw<br />

what exists at those coordinates in the gradi<strong>en</strong>t matrix. Ev<strong>en</strong> if you use one of the shape methods of a Graphics object<br />

such as drawRect(), the gradi<strong>en</strong>t does not stretch itself to the size of the shape that is drawn—the gradi<strong>en</strong>t’s size must<br />

be specified in the gradi<strong>en</strong>t matrix itself.<br />

The following illustrates the visual differ<strong>en</strong>ce betwe<strong>en</strong> the dim<strong>en</strong>sions of the gradi<strong>en</strong>t matrix and the dim<strong>en</strong>sions of<br />

the draw itself:<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(100, 40, 0, 0, 0);<br />

myShape.graphics.beginGradi<strong>en</strong>tFill(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, 50, 40);<br />

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

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

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

this.addChild(myShape);<br />

This code draws three gradi<strong>en</strong>ts with the same fill style, specified with an equal distribution of red, gre<strong>en</strong>, and blue.<br />

The gradi<strong>en</strong>ts are drawn using the drawRect() method with pixel widths of 50, 100, and 150 respectively. The gradi<strong>en</strong>t<br />

matrix which is specified in the beginGradi<strong>en</strong>tFill() method is created with a width of 100 pixels. This means that<br />

the first gradi<strong>en</strong>t will <strong>en</strong>compass only half of the gradi<strong>en</strong>t spectrum, the second will <strong>en</strong>compass all of it, and the third<br />

will <strong>en</strong>compass all of it and have an additional 50 pixels of blue ext<strong>en</strong>ding to the right.<br />

Last updated 6/6/2012<br />

229

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

Saved successfully!

Ooh no, something went wrong!