<< Chapter < Page | Chapter >> Page > |
<?xml version="1.0" encoding="utf-8"?><!--
KeyboardEvent02See explanation in the file named Driver.as
--><mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:Driver/></mx:Application>
As is often the case in this series of lessons, the MXML file is very simple because the program was coded almost entirely in ActionScript. The MXML codesimply instantiates an object of the Driver class. From that point forward, the behavior of the program is controlled by ActionScript code.
Beginning of the Driver class
The driver class begins in Listing 2.
/*KeyboardEvent02 06/03/10
Illustrates the use of KeyboardEvent, charCode values,absolute positioning on a Canvas object, focus, and a
TextArea object among other things.See http://livedocs.adobe.com/flex/3/langref/flash/events
/KeyboardEvent.html*********************************************************/
package CustomClasses{import flash.events.KeyboardEvent;
import flash.events.MouseEvent;import mx.containers.Canvas;
import mx.controls.Label;import mx.controls.TextArea;
public class Driver extends Canvas{//Instantiate and save references to the
// objects needed by the program.private var instrArea:TextArea = new TextArea();
private var targetLabel:Label = new Label();private var canvasLabel:Label = new Label();
Extending the Canvas class
With the possible exception of the fact that the Driver class extends the Canvas class, there is nothing new in Listing 2. An object of the Driver class is a Canvas object.
I elected to extend the Canvas class because this makes it possible to position objects added as children of that class using absolutelocation coordinates.
The constructor for the Driver class
The constructor is shown in its entirety in Listing 3.
public function Driver() {//constructor
//Set the size of the Canvas object.this.width = 300;
this.height = 120;//Prepare the TextArea and the labels.canvasLabel.text = "This is a 300x120 Canvas";
instrArea.text = "First click the yellow canvas "+ "with the mouse\nThen press a key to display "
+ "the character.";instrArea.width = 298;
instrArea.height = 40;instrArea.x = 1;
instrArea.y = 26;targetLabel.setStyle("fontSize", 30);targetLabel.x = 10;
targetLabel.y = 78//Display an empty string at startup.
targetLabel.text = "";//Add the labels and TextArea to the Canvas.this.addChild(canvasLabel);
this.addChild(instrArea);this.addChild(targetLabel);//Set the Canvas background color to yellow.
this.setStyle("backgroundColor", "0xFFFF00");//Register two event listeners on the canvas.this.addEventListener(
MouseEvent.CLICK, clickHandler);this.addEventListener(
KeyboardEvent.KEY_DOWN,eventHandler);} //end constructor
There are several things in Listing 3 that deserve an explanation beyond the embedded comments.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?