16 February 2009
General experience with programming in ActionScript 3 is suggested.
Beginning
The Adobe Flash CS4 Professional User Interface components are ActionScript 3.0 class objects that implement commonly used pieces of the user interface of a web page. These components help you accelerate development of your web pages by providing elements that you can easily plug in and customize. Flash CS4 provides the following user interface components:
| Component | Description |
|---|---|
| Button | A resizable, rectangular button that a user can click or press to initiate an action. For example, you can use a Button to send the user to a particular location, such as forward to a new web page or back to a previous one, or to indicate a choice as with an OK or a Submit button. |
| CheckBox | A square box that a user can click to add a check mark to indicate a choice or selection. |
| ColorPicker | A box that opens to allow the user to choose a color from a palette of color swatches. |
| ComboBox | A combination of a text field and a drop-down list from which a user can select an item. |
| DataGrid | A grid of rows and columns for displaying data, which you can load from an array or an XML file that you supply in the form of a DataProvider. |
| Label | A single line of text, typically to identify an element on a web page. |
| List | A scrollable list box from which a user can make one or more selections. |
| NumericStepper | Allows a user to step through an ordered set of numbers. Can be used for selecting dates or specifying a quantity or amount. |
| ProgressBar | Indicates the progress of loading content to the web page. |
| RadioButton | Round buttons that are provided in a group from which a user can select only one. Selecting one RadioButton unselects the previously selected RadioButton in the group. |
| ScrollPane | For displaying content in a scrollable pane when the content would take up too much space on the web page. |
| Slider | Lets a user select a value by sliding a graphical thumb between the end points of a track that corresponds to a range of values. |
| TextArea | Can display or receive multiple lines of text and wrap long lines of text. |
| TextInput | Receives a single line of text input by a user. |
| TileList | A list that is made up of rows and columns that are supplied with data by a DataProvider. |
| UILoader | A container that can display SWF, JPEG, progressive JPEG, PNG, and GIF files. |
| UIScrollBar | Allows you to add a scroll bar to a text field. |
This article covers the following topics: adding user interface components to your Flash application, setting component properties and parameters, changing a component's default size and its location on the Stage, and handling events. It concludes with a small shopping cart example.
There are two ways to add a component to a Flash application:
The following procedure shows how to add a Button component to your Flash application during development. You can use the same process to add any of the User Interface components to your application.
To get the source files for this example, download ButtonExample.zip at the top of this page.
import statement to the Actions panel on the first frame of the Timeline to make the component's class available to your application. For the Button component, you would add the following import statement:import fl.controls.Button;
var aButton:Button = new Button();
addChild() method to add the component instance to the Stage or application container. For example, the following statement adds the Button instance aButton to the current display container:addChild(aButton);
aButton.label = "Accept";
Each component's class has properties that you can read to obtain information about the component or write to affect the component's appearance and behavior. Generally, you set generic properties common to all components, such as position, dimension, color effect, and blending mode in the Property inspector. You set component-specific properties in the Parameters tab of the Component inspector. When you set a parameter in the Flash authoring tool, you are setting the corresponding property in the ActionScript class.
You can directly access and set a component's properties using ActionScript. Setting a property in ActionScript overrides a value that was set using the Property inspector or the Component inspector.
Note: To assign a name to a component instance you must use either the Property inspector or ActionScript
editable property to true to make the text field editable. Or you could set rowCount to set the maximum number of rows that can appear in the drop-down list.In ActionScript, you use the dot (.) operator to access a component's properties. For example, to implement word wrapping for a Label called titleLabel, you would write the following line of ActionScript code to set the wordWrap property:
titleLabel.wordWrap = true;
To place the Label 100 pixels from the left of the Stage and 50 pixels from the top, you could add the following code to set the x and y properties:
titleLabel.x = 100;
titleLabel.y = 50;
You would place this code in the Actions panel (Window > Actions) as shown below:
You can change a component's size and location on the Stage in the following ways:
The easiest way to change a component's default size is by using the Free Transform tool, located in the Tools panel (Window > Tools). The Free Transform tool looks like this:
To change a component's location on the Stage using the Flash authoring tool, simply click the component and drag it to the desired location. When you release the component, you can see the x (horizontal) and y (vertical) coordinates of the new location in the Property inspector. You can make finer adjustments using the keyboard. You can make horizontal adjustments of one pixel by holding down the Ctrl key (Windows) or the Command key (Mac OS) and pressing the left or right arrow keys. You can adjust the vertical location 10 pixels at a time by holding down the Shift key and pressing the up arrow or down arrow keys.
You can also specify a component's size and location by setting parameters in the Property inspector.
Each component's dimensions are shown as width (W) and height (H) values in the Property inspector. You can change the size of a component instance by changing the values of these W and H parameters.
Each component's location on the Stage is shown as x (horizontal) and y (vertical) coordinates in the Property inspector. These parameters correspond to the class's x and y properties. The horizontal position represents the number of pixels from the left edge of the Stage and the vertical position represents the number of pixels from the top. You can change the location of a component instance on the Stage by changing the values of the X and Y parameters.
You can also specify a component's size and location using ActionScript.
You can change the size of a component instance by setting its height and width properties and you can change its location by setting its x and y properties. For example, the following lines of code specify the size and location of a List component called citiesList:
citiesList.height = 100;
citiesList.width = 125;
citiesList.x = 100;
citiesList.y = 50;
Components communicate with the application that contains them by dispatching events. Events are noteworthy occurrences in your Flash application that you might want to handle with ActionScript code. For example, an event occurs when a user clicks a button or enters characters in a text field. Events also occur when a user scrolls using the UIScrollBar component and when the UILoader component completes a loading operation. Each component class defines the events that occur for that component. The ColorPicker class, for example, has the following events, in addition to the events that it inherits from its parent classes:
| Event | Descripton |
|---|---|
change |
Dispatched when the user clicks a color in the palette. |
close |
Dispatched when the user closes the color palette. |
enter |
Dispatched when the user presses the Enter key after editing the internal text field of the ColorPicker component. |
itemRollOut |
Dispatched when the user rolls out of a swatch in the color palette. |
itemRollOver |
Dispatched when the user rolls over a swatch in the color palette. |
open |
Dispatched when the user opens the color palette. |
Handling an event refers to taking an action in response to the event. Handling an event consists of listening for the event and executing the actions you want to take in response.
To listen for an event you must add an event listener for the component instance on which the event occurs. In ActionScript 3.0, use the addEventListener() method to add an event listener. When you add an event listener, you specify the event that you are listening for and the name of a function to call when the event occurs. The function is the event handler, which you must provide. For example, the following line of code adds an event listener that listens for a change event on a List instance called laundryList:
laundryList.addEventListener(Event.CHANGE, howMuch);
The first parameter, Event.CHANGE, is a constant in the Event class that specifies the name of the change event. The second parameter, howMuch, is the name of the function to call when the event occurs.
When a change event does occur on the laundryList instance, Flash calls the function howMuch() and passes it an event object that contains information about the event. The base event object is defined by the Event class.
To implement the event handling function howMuch(), define the function and add the logic that you want to execute. The following implementation of the howMuch() function accesses the data field of the selected list item and adds it to a variable called total. The function casts selectedItem.data to a Number type because in the Event object the data property is defined as a String and the operation would generate an error otherwise:
var total:Number = 0;
function howMuch(event_obj:Event):void {
total += Number(event.target.selectedItem.data);
}
The following example creates an abbreviated shopping cart application using Flash CS4 User Interface components. It uses Label instances for all of the text prompts. It uses a variety of other components to build the user interface, including:
The item description is provided through the text parameter of the item TextArea (itemTa). The data in the Size (sizeCb) and Shipping (shipCb) ComboBoxes is created during authoring by double clicking the value cell for the dataProvider parameters in the Property inspector and adding items to the Values collection dialog. The values seen in the Size ComboBox were added simply by filling in the label parameter with the values that the ComboBox displays. The following illustration shows the values provided to the dataProvider parameter for the Shipping ComboBox.
The example also requires the following ActionScript code, which:
MouseEvent.CLICK event on the Checkout buttoncheckOut() function to perform the following tasks when the event occurs: total variable to calculate the total amout due for the ordertext property of the checkout summary TextArea (summaryTxt)total variable to summaryTxt.textsummaryTxt) visibleHere is the source code for the example application. Note that this source code depends on having a number of components already on the stage. Please download the source file to see how the components are placed.
// Sets colors for the ColorPicker
itemCp.colors = [0x000000, 0x990000, 0x333366, 0x336633];
// Hides the text input field for the color picker
itemCp.showTextField = false;
// Hides the checkout summary text
summaryTxt.visible = false;
// Listens for a click on the checkout button
checkoutBtn.addEventListener(MouseEvent.CLICK, checkOut);
/**
* Assembles the checkout summary text.
*/
function checkOut(event_obj:MouseEvent):void
{
//
var total:Number = 0;
// First makes sure a size is selected
if(sizeCb.selectedItem == null)
{
summaryTxt.text = "Please select a size!";
summaryTxt.visible = true;
return;
}
summaryTxt.text = String(qtyNs.value) + " ";
// Identifies the item as men's or women's
if(mensRb.selected)
{
summaryTxt.text += mensRb.label + " ";
}
else
{
summaryTxt.text += womensRb.label + " ";
}
summaryTxt.text += sizeCb.selectedItem.label + " ";
switch(itemCp.hexValue)
{
// Adds the color choice
case "990000" :
summaryTxt.text += "Burgundy ";
break;
case "333366" :
summaryTxt.text += "Navy ";
break;
case "336633" :
summaryTxt.text += "Forest ";
break;
default :
summaryTxt.text += "Black ";
}
summaryTxt.text += itemDescTxt.text + "\n";
summaryTxt.text += "Total: " + String(qtyNs.value) + " x 259.95 + ";
summaryTxt.text += shipCb.selectedItem.data + " (Shipping) = $";
total = qtyNs.value * 259.95 + Number(shipCb.selectedItem.data);
summaryTxt.text += String(total.toFixed(2));
// Shows the checkout summary
summaryTxt.visible = true;
}
stop();
To get the source files for this example, download SmallShoppingCart.zip at the top of this page.
For more information about this topic, see Using ActionScript 3.0 Components and the ActionScript 3.0 Language and Components Reference.