User Guide

92 Button component
4. Select Control > Test Movie.
5. When you click the button, Flash displays the message “You clicked the button!”.
To create a Button using ActionScript:
1. Drag the Button component from the Components panel to the current document’s
library.
This adds the component to the library, but doesnt make it visible in the application.
2. In the first frame of the main timeline, add the following ActionScript to the Actions panel
to create a Button instance:
this.createClassObject(mx.controls.Button, "my_button", 10,
{label:"Click me"});
my_button.move(20, 20);
The method UIObject.createClassObject() is used to create the Button instance
named my_button and specify a label property. Then, the code uses the method
UIObject.move() to position the button.
3. Now, add the following ActionScript to create an event listener and an event handler
function:
function clicked() {
trace("You clicked the button!");
}
my_button.addEventListener("click", clicked);
This uses the method “EventDispatcher.addEventListener()” on page 501 with a custom
function to handle the event.
4. Select Control > Test Movie.
5. When you click the button, Flash displays the message “You clicked the button!”.
As you use the Button component with other components, you can create more sophisticated
event handler functions. In this example, the “click” event causes the Accordion component to
change the display of the panels.
To use a Button event with another component:
1. Drag the Button component from the Components panel to the current document’s
library.
This adds the component to the library, but doesnt make it visible in the application.
2. Drag the Accordion component from the Components panel to the current document’s
library.