User Guide
Adding components to Flash documents 53
Alternatively, you can use the full package path, as follows:
mx.controls.Alert.show("The connection has failed", "Error");
For more information, see “About importing class files” in Learning ActionScript 2.0 in Flash.
You can use ActionScript methods to set additional parameters for dynamically added
components. For more information, see Components Language Reference.
To add a component to your Flash document using ActionScript:
1. Drag a component from the Components panel into the library for the current document.
2. Select the frame in the Timeline where you want to add the component.
3. Open the Actions panel if it isn’t already open.
4. Call createClassObject() to create the component instance at runtime.
This method can be called on its own, or from any component instance. The
createClassObject() method takes the following parameters: a component class name,
an instance name for the new instance, a depth, and an optional initialization object that
you can use to set properties at runtime.
You can specify the class package in the class name parameter, as in the following example:
createClassObject(mx.controls.CheckBox, "cb", 5, {label:"Check Me"});
Alternatively, you can import the class package, as in the following example:
import mx.controls.CheckBox;
createClassObject(CheckBox, "cb", 5, {label:"Check Me"});
For more information, see UIObject.createClassObject() on page 1323 and Chapter 4,
“Handling Component Events,” on page 63.
NOTE
To add a component to a document at runtime, it must be in the library when the SWF
file is compiled. To add a component to the library, drag the component icon from the
Components panel to the library. Furthermore, if you are loading a movie clip containing
a dynamically instantiated (using ActionScript) component into another movie clip, the
parent movie clip must have the component in the library when the SWF file is compiled.
NOTE
Components are set to Export in First Frame by default (right-click for Windows, or
control-click for Macintosh, and select the Linkage menu option to see the Export in
First Frame setting). If you want to use a preloader for an application containing
components, you need to change the export frame, see “Using a preloader with
components” on page 60 for instructions.