Flash CS4 ActionScript 3.0 Tutorial
How to Add Pre-Built Label Component to Your Flash File
Flash ships with several Components. Components are pre-packaged objects that allow you to quickly add elements, such as scroll bars, text boxes and buttons.
Components can be added to the stage using the drag and drop method from the Components panel or added with ActionScript. The Component parameters are altered with the Component Inspector or ActionScript.
This example shows how to add a Label Component using the drag and drop method and with ActionScript. It also describes changing the Component parameters with the Component Inspector and ActionScript. To talk to a Component with ActionScript, it must first be added to the Library.
Components are actually Movie Symbol objects regardless of the Component. If you are developing in ActionScript 2.0, you will see a different set of Components available.
For more information on the other Components, see topics, Button Component, CheckBox Component, ColorPicker Component, ComboBox Component, DataGrid Component, List Component, NumericStepper Component, ProgressBar Component, RadioButton Component, ScrollPane Component, Slider Component, TextArea Component, TextInput Component, TileList Component, UILoader Component and UIScrollBar Component.
Label Component with instance name label01_mc
Open a new Flash document and add a new layer.
Name the upper layer Actions and the lower one Content.
Select the first frame of the Content layer and open the Components panel (Window > Components).
Double-click the Label Component icon to a add a Label to the center of the stage. The Label and it's assets are also added to the Library.
With the Label selected open the Properties panel and change it's x and y settings to 50 and 50.
With the Label still selected, open the Component Inspector (Window > Component Inspector).
Click the text row in the value column and type in This is my first label.
With the CheckBox still selected, open the Properties panel and assign it an instance name of label01_mc.
Open the ActionScript panel for the only keyframe in the Actions layer by Option double-clicking (PC - Alt double-clicking) the keyframe.
Copy/Paste the following code in the ActionScript Panel:
var label02_mc:Label=new Label();
label02_mc.text="This is my second label";
Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The two labels appear on the stage.
Dissecting the Code
The first line of the code at Step Ten imports the code necessary to talk to the Label class. Line two establishes a new Label variable named Label02.
Lines three through six set the Label's text, width and stage position. The final line adds it to the stage.
More Component Options
In this example we showed how to change the Label's text. You can use this same method to change the other Label values shown in the Component Inspector (i.e., autoSize, condenseWhite, enabled, htmlText, selectable, visible and wordWrap). You can also alter the height by changing the height properties (i.e., label01_mc.height=200;).