User Guide
Example: SpriteArranger 191
Adding display objects to the canvas
When the user clicks the Add Shape button, the application calls the addShape() method of
the DrawingCanvas class. It instantiates a new GeometricSprite by calling the appropriate
constructor function of one of the GeometricSprite subclasses, as the following example
shows:
public function addShape(shapeName:String, len:Number):void
{
var newShape:GeometricSprite;
switch (shapeName)
{
case "Triangle":
newShape = new TriangleSprite(len);
break;
case "Square":
newShape = new SquareSprite(len);
break;
case "Circle":
newShape = new CircleSprite(len);
break;
}
newShape.alpha = 0.8;
this.addChild(newShape);
}
Each constructor method calls the drawShape() method, which uses the graphics property
of the class (inherited from the Sprite class) to draw the appropriate vector graphic. For
example, the
drawShape() method of the CircleSprite class includes the following code:
this.graphics.clear();
this.graphics.lineStyle(1.0, this.lineColor, 1.0);
this.graphics.beginFill(this.fillColor, 1.0);
var radius:Number = this.size / 2;
this.graphics.drawCircle(radius, radius, radius);
The second to last line of the addShape() function sets the alpha property of the display
object (inherited from the DisplayObject class), so that each display object added to the
canvas is slightly transparent, letting the user see the objects behind it.
The final line of the
addChild() method adds the new display object to the child list of the
instance of the DrawingCanvas class, which is already on the display list. This causes the new
display object to appear on the Stage.