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

Display programming<br />

public function addShape(shapeName:String, l<strong>en</strong>:Number):void<br />

{<br />

var newShape:GeometricSprite;<br />

switch (shapeName)<br />

{<br />

case "Triangle":<br />

newShape = new TriangleSprite(l<strong>en</strong>);<br />

break;<br />

}<br />

case "Square":<br />

newShape = new SquareSprite(l<strong>en</strong>);<br />

break;<br />

case "Circle":<br />

newShape = new CircleSprite(l<strong>en</strong>);<br />

break;<br />

}<br />

newShape.alpha = 0.8;<br />

this.addChild(newShape);<br />

Each constructor method calls the drawShape() method, which uses the graphics property of the class (inherited<br />

from the Sprite class) to draw the appropriate vector graphic. For example, the drawShape() method of the<br />

CircleSprite class includes the following code:<br />

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

this.graphics.lineStyle(1.0, this.lineColor, 1.0);<br />

this.graphics.beginFill(this.fillColor, 1.0);<br />

var radius:Number = this.size / 2;<br />

this.graphics.drawCircle(radius, radius, radius);<br />

The second to last line of the addShape() function sets the alpha property of the display object (inherited from the<br />

DisplayObject class), so that each display object added to the canvas is slightly transpar<strong>en</strong>t, letting the user see the<br />

objects behind it.<br />

The final line of the addChild() method adds the new display object to the child list of the instance of the<br />

DrawingCanvas class, which is already on the display list. This causes the new display object to appear on the Stage.<br />

The interface for the application includes two text fields, selectedSpriteTxt and outputTxt. The text properties of<br />

these text fields are updated with information about the GeometricSprite objects that have be<strong>en</strong> added to the canvas<br />

or selected by the user. The GeometricSprite class handles this information-reporting task by overriding the<br />

toString() method, as follows:<br />

public override function toString():String<br />

{<br />

return this.shapeType + " of size " + this.size + " at " + this.x + ", " + this.y;<br />

}<br />

The shapeType property is set to the appropriate value in the constructor method of each GeometricSprite subclass.<br />

For example, the toString() method might return the following value for a CircleSprite instance rec<strong>en</strong>tly added to<br />

the DrawingCanvas instance:<br />

Circle of size 50 at 0, 0<br />

The describeChildr<strong>en</strong>() method of the DrawingCanvas class loops through the canvas’s child list, using the<br />

numChildr<strong>en</strong> property (inherited from the DisplayObjectContainer class) to set the limit of the for loop. It g<strong>en</strong>erates<br />

a string listing each child, as follows:<br />

Last updated 6/6/2012<br />

206

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

Saved successfully!

Ooh no, something went wrong!