User Guide
192 Display Programming
The interface for the application includes two text fields, selectedSpriteTxt and
outputTxt. The text properties of these text fields are updated with information about the
GeometricSprite objects that have been added to the canvas or selected by the user. The
GeometricSprite class handles this information-reporting task by overriding the
toString()
method, as follows:
public override function toString():String
{
return this.shapeType + “ of size “ + this.size + " at " + this.x + ", "
+ this.y;
}
The shapeType property is set to the appropriate value in the constructor method of each
GeometricSprite subclass. For example, the
toString() method might return the following
value for a CircleSprite instance recently added to the DrawingCanvas instance:
Circle of size 50 at 0, 0
The describeChildren() method of the DrawingCanvas class loops through the canvas’s
child list, using the
numChildren property (inherited from the DisplayObjectContainer class)
to set the limit of the
for loop. It generates a string listing each child, as follows:
var desc:String = "";
var child:DisplayObject;
for (var i:int = 0; i < this.numChildren; i++)
{
child = this.getChildAt(i);
desc += i + ": " + child + '\n';
}
The resulting string is used to set the text property of the outputTxt text field.
Clicking and dragging display objects
When the user clicks on a GeometricSprite instance, the application calls the onMouseDown()
event handler. As the following shows, this event handler is set to listen for mouse down
events in the constructor function of the GeometricSprite class:
this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
The onMouseDown() method then calls the showSelected() method of the GeometricSprite
object. If it is the first time this method has been called for the object, the method creates a
new Shape object named
selectionIndicator and it uses the graphics property of the
Shape object to draw a red highlight rectangle, as follows:
this.selectionIndicator = new Shape();
this.selectionIndicator.graphics.lineStyle(1.0, 0xFF0000, 1.0);