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

The DrawingCanvas class<br />

In Flex, all child display objects added to a Container object must be of a class that desc<strong>en</strong>ds from the<br />

mx.core.UICompon<strong>en</strong>t class. This application adds an instance of the DrawingCanvas class as a child of an<br />

mx.containers.VBox object, as defined in MXML code in the SpriteArranger.mxml file. This inheritance is defined in<br />

the DrawingCanvas class declaration, as follows:<br />

public class DrawingCanvas ext<strong>en</strong>ds UICompon<strong>en</strong>t<br />

The UICompon<strong>en</strong>t class inherits from the DisplayObject, DisplayObjectContainer, and Sprite classes, and the code in<br />

the DrawingCanvas class uses methods and properties of those classes.<br />

The DrawingCanvas class ext<strong>en</strong>ds the Sprite class, and this inheritance is defined in the DrawingCanvas class<br />

declaration, as follows:<br />

public class DrawingCanvas ext<strong>en</strong>ds Sprite<br />

The Sprite class is a subclass of the DisplayObjectContainer and DisplayObject classes, and the DrawingCanvas class<br />

uses methods and properties of those classes.<br />

The DrawingCanvas() constructor method sets up a Rectangle object, bounds, which is property that is later used in<br />

drawing the outline of the canvas. It th<strong>en</strong> calls the initCanvas() method, as follows:<br />

this.bounds = new Rectangle(0, 0, w, h);<br />

initCanvas(fillColor, lineColor);<br />

AS the following example shows, the initCanvas() method defines various properties of the DrawingCanvas object,<br />

which were passed as argum<strong>en</strong>ts to the constructor function:<br />

this.lineColor = lineColor;<br />

this.fillColor = fillColor;<br />

this.width = 500;<br />

this.height = 200;<br />

The initCanvas() method th<strong>en</strong> calls the drawBounds() method, which draws the canvas using the DrawingCanvas<br />

class’s graphics property. The graphics property is inherited from the Shape class.<br />

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

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

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

this.graphics.drawRect(bounds.left - 1,<br />

bounds.top - 1,<br />

bounds.width + 2,<br />

bounds.height + 2);<br />

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

The following additional methods of the DrawingCanvas class are invoked based on user interactions with the<br />

application:<br />

The addShape() and describeChildr<strong>en</strong>() methods, which are described in “Adding display objects to the<br />

canvas” on page 205<br />

The moveToBack(), moveDown(), moveToFront(), and moveUp() methods, which are described in “Rearranging<br />

display object layering” on page 208<br />

The onMouseUp() method, which is described in “Clicking and dragging display objects” on page 207<br />

Last updated 6/6/2012<br />

204

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

Saved successfully!

Ooh no, something went wrong!