<< Chapter < Page | Chapter >> Page > |
<?xml version="1.0" encoding="utf-8"?><!--Illustrates polymorphism.--><mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:Driver/></mx:Application>
In keeping with my objective of emphasizing ActionScript programming rather than Flex MXML programming in this series of tutorial lessons, this program iswritten almost entirely in ActionScript. The MXML file serves simply as a launch pad for the program by instantiating an object of the class named Driver .
A complete listing of this file is provided in Listing 10 near the end of the lesson. The class named Driver begins in Listing 2.
package CustomClasses{
import flash.events.*;import mx.containers.VBox;import mx.controls.Button;
import mx.controls.Label;import mx.controls.TextArea;public class Driver extends VBox{
private var textArea:TextArea = new TextArea();
A Driver object is a VBox object
The class named Driver extends the VBox class. As a result, components added to the objects are arranged vertically with leftjustification as shown in Figure 2.
The TextArea object
Listing 2 instantiates the TextArea object shown in Figure 2 and saves its reference in an instance variable named textArea . The reference was saved in an instance variable to make it available to both the constructorand a click event handler method later.
The constructor for the class named Driver
The constructor for the class named Driver is shown in Listing 3.
public function Driver(){//constructor
var label:Label = new Label();label.text = "Polymorphism Demonstration";
label.setStyle("fontSize",14);label.setStyle("color",0xFFFF00);
addChild(label);var button:Button = new Button();button.label = "GO";addChild(button);textArea.width = 200;
textArea.height = 70;addChild(textArea);button.addEventListener(
MouseEvent.CLICK,buttonHandler);}//end constructor
Nothing new here
I don't believe there is anything in Listing 3 that you haven't seen in previous lessons. The code in the constructor simply constructs the GUIpictured in Figure 2.
A click event handler method
Probably the most important thing to note about listing 3 is the registration of the click event handler method named buttonHandler on the button shown in Figure 2. Once the GUI is constructed, further action occurs only when the user clicks the button, causingthe method named buttonHandler to be executed.
Beginning of the method named buttonHandler
The method named buttonHandler begins in Listing 4. This method is executed each time the user clicks the button shown in Figure 2.
private function buttonHandler(
event:MouseEvent):void{var randomChoice:Number = Math.random();var radius:uint = uint(10*Math.random() + 1);
var width:uint = uint(10*Math.random() + 1);var height:uint = uint(10*Math.random() + 1);
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?