<< Chapter < Page Chapter >> Page >

Code for the file named main.mxml.

<?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.

The ActionScript code

Beginning of the Driver class

The driver class begins in Listing 2.

Beginning of the driver class.

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

The constructor for the driver class.

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.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask