Using ActionScript 3.0 Components |
|
|
|
| About ActionScript 3.0 Components > A simple application > Creating the Greetings application | |||
The following steps create the Greetings application using the Flash authoring tool to create a FLA file, place components on the Stage, and add ActionScript code to the Timeline.
To create the Greetings application in a FLA file:A new Flash window opens.
for the instance name, and enter the following information:
import flash.events.Event;
import fl.events.ComponentEvent;
import fl.events.ColorPickerEvent;
import fl.controls.RadioButtonGroup;
var rbGrp:RadioButtonGroup = RadioButtonGroup.getGroup("fontRbGrp");
rbGrp.addEventListener(MouseEvent.CLICK, rbHandler);
txtCp.addEventListener(ColorPickerEvent.CHANGE,cpHandler);
msgCb.addEventListener(Event.CHANGE, cbHandler);
The first three lines import the event classes that the application uses. An event occurs when a user interacts with one of the components. The next five lines register event handlers for the events that the application wants to listen for. A click event occurs for a RadioButton when a user clicks on it. A change event occurs when a user selects a different color in the ColorPicker. A change event occurs on the ComboBox when a user chooses a different greeting from the drop-down list.
The fourth line imports the RadioButtonGroup class so that the application can assign an event listener to the group of RadioButtons, rather than assigning the listener to each button individually.
tf TextFormat object, which the application uses to change the size and color style properties of the text in the TextArea.
var tf:TextFormat = new TextFormat();
rbHandler event handling function. This function handles a click event when a user clicks on one of the RadioButton components.
function rbHandler(event:MouseEvent):void {
switch(event.target.selection.name) {
case "smallRb":
tf.size = 14;
break;
case "largerRb":
tf.size = 18;
break;
case "largestRb":
tf.size = 24;
break;
}
aTa.setStyle("textFormat", tf);
}
This function uses a switch statement to examine the target property of the event object to determine which RadioButton triggered the event. The currentTarget property contains the name of the object that triggered the event. Depending on which RadioButton the user clicked, the application changes the size of the text in the TextArea to 14, 18, or 24 points.
cpHandler() function, which handles a change to the value in the ColorPicker:
function cpHandler(event:ColorPickerEvent):void {
tf.color = event.target.selectedColor;
aTa.setStyle("textFormat", tf);
}
This function sets the color property of the tf TextFormat object to the color selected in the ColorPicker and then calls setStyle() to apply it to the text in the aTa TextArea instance.
cbHandler() function, which handles a change to the selection in the ComboBox:
function cbHandler(event:Event):void {
aTa.text = event.target.selectedItem.label;
}
This function simply replaces the text in the TextArea with the selected text in the ComboBox, event.target.selectedItem.label.
The following section shows you how to build the same application with an external ActionScript class and a FLA file that has only the required components in the library.
To create the Greetings2 application with an external class file:A new Flash window opens.
The compiled SWF file will use each of these assets, so you need to add them to the library. Drag the components to the bottom of the Library panel. As you add these components to the library, other assets (such as List, TextInput, and UIScrollBox) are added automatically.
If Flash displays a warning, saying that "a definition for the document class could not be found," ignore it. You will define the Greetings2 class in the following steps. This class defines the main functionality for the application.
A new script window opens.
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextFormat;
import fl.events.ComponentEvent;
import fl.events.ColorPickerEvent;
import fl.controls.ColorPicker;
import fl.controls.ComboBox;
import fl.controls.RadioButtonGroup;
import fl.controls.RadioButton;
import fl.controls.TextArea;
public class Greetings2 extends Sprite {
private var aTa:TextArea;
private var msgCb:ComboBox;
private var smallRb:RadioButton;
private var largerRb:RadioButton;
private var largestRb:RadioButton;
private var rbGrp:RadioButtonGroup;
private var txtCp:ColorPicker;
private var tf:TextFormat = new TextFormat();
public function Greetings2() {
The script defines an ActionScript 3.0 class, named Greetings2. The script does the following:
tf TextFormat object.Greetings2(), for the class. We will add lines to this function, and add other methods to the class in the following steps.Greeting2() function:
createUI();
setUpHandlers();
}
The function should now look like the following:
public function Greetings2() {
createUI();
setUpHandlers();
}
Greeting2() method:
private function createUI() {
bldTxtArea();
bldColorPicker();
bldComboBox();
bldRadioButtons();
}
private function bldTxtArea() {
aTa = new TextArea();
aTa.setSize(230, 44);
aTa.text = "Hello World!";
aTa.move(165, 57);
addChild(aTa);
}
private function bldColorPicker() {
txtCp = new ColorPicker();
txtCp.move(96, 72);
addChild(txtCp);
}
private function bldComboBox() {
msgCb = new ComboBox();
msgCb.width = 130;
msgCb.move(265, 120);
msgCb.prompt = "Greetings";
msgCb.addItem({data:"Hello.", label:"English"});
msgCb.addItem({data:"Bonjour.", label:"Français"});
msgCb.addItem({data:"¡Hola!", label:"Español"});
addChild(msgCb);
}
private function bldRadioButtons() {
rbGrp = new RadioButtonGroup("fontRbGrp");
smallRb = new RadioButton();
smallRb.setSize(100, 22);
smallRb.move(155, 120);
smallRb.group = rbGrp; //"fontRbGrp";
smallRb.label = "Small";
smallRb.name = "smallRb";
addChild(smallRb);
largerRb = new RadioButton();
largerRb.setSize(100, 22);
largerRb.move(155, 148);
largerRb.group = rbGrp;
largerRb.label = "Larger";
largerRb.name = "largerRb";
addChild(largerRb);
largestRb = new RadioButton();
largestRb.setSize(100, 22);
largestRb.move(155, 175);
largestRb.group = rbGrp;
largestRb.label = "Largest";
largestRb.name = "largestRb";
addChild(largestRb);
}
These lines do the following:
addChild() method.bldRadioButtons() method, add the following code for the setUpHandlers() method:
private function setUpHandlers():void {
rbGrp.addEventListener(MouseEvent.CLICK, rbHandler);
txtCp.addEventListener(ColorPickerEvent.CHANGE,cpHandler);
msgCb.addEventListener(Event.CHANGE, cbHandler);
}
private function rbHandler(event:MouseEvent):void {
switch(event.target.selection.name) {
case "smallRb":
tf.size = 14;
break;
case "largerRb":
tf.size = 18;
break;
case "largestRb":
tf.size = 24;
break;
}
aTa.setStyle("textFormat", tf);
}
private function cpHandler(event:ColorPickerEvent):void {
tf.color = event.target.selectedColor;
aTa.setStyle("textFormat", tf);
}
private function cbHandler(event:Event):void {
aTa.text = event.target.selectedItem.data;
}
}
}
These functions define event listeners for the components.
|
|
|
|