To add a component to a document at run time
with ActionScript, the component must first be in the application’s
library (Window > Library) when the SWF file is compiled.
To add a component to the library, drag the component from the Components
panel into the Library panel. For more information on the library,
see
The library
.
You must also import the component’s class file to make its API
available to your application. Component class files are installed
in
packages
that contain one or more classes. To import a
component class, use the
import
statement and specify
the package name and class name. You would import the Button class,
for example, with the following
import
statement:
import fl.controls.Button;
For information on what package a component is in, see the
ActionScript 3.0 Reference for the Adobe
Flash Platform
. For information about the location of component
source files, see
Working with component files
.
To create an instance of the component, you must invoke the component’s ActionScript
constructor method. For example, the following statement creates an
instance of a Button called
aButton
:
var aButton:Button = new Button();
The final step is to call the static
addChild()
method
to add the component instance to the Stage or application container.
For example, the following statement adds the
aButton
instance:
addChild(aButton);
At this point, you can use the component’s API to dynamically
specify the component’s size and position on the Stage, listen for
events, and set properties to modify its behavior. For more information
on the API for a particular component, see the
ActionScript 3.0 Reference for the Adobe
Flash Platform
.
For more information on the
addChild()
method,
see
Work with the display list
.