Creating a new Label instance using ActionScript 3.0

by Peter deHaan on December 5, 2008

in Label

To create a new instance of the Label class using ActionScript, drag a copy of the component from the Components panel into your current Flash document’s library. Next, import the fl.controls.Label class using the import statement. This step is required because the component files are not implicitly imported, like the other flash.* packages. Once you import the Label class into your current document, you can create a new Label instance using the new operator. Finally, add the new Label instance to the display list using the addChild() method.

Full code after the jump.

// ActionScript 3.0
import fl.controls.Label;
 
var myLabel:Label = new Label();
myLabel.text = "Hello world";
myLabel.move(10, 10);
addChild(myLabel);

For more information on the Flash/ActionScript 3.0 Label component, see the “Using the Label component” Flash Quick Start on Adobe.com.

{ 3 comments… read them below or add one }

1 Raj Kumar Kundu 12.08.08 at 3:03 am

For adding Labels in Action Script 3.0 it says the following:

// ActionScript 3.0
import fl.controls.Label;
 
var myLabel:Label = new Label();
myLabel.text = "Hello world";
myLabel.move(10, 10);
addChild(myLabel);

But the fl.controls.Label is not available. I am using Flex Builder as IDE. Is there anything I am missing?

2 Peter deHaan 12.08.08 at 10:03 am

Raj Kumar Kundu,

Sorry, I don’t know how you’d do this in Flex Builder. I was using Flash Authoring CS3/CS4 and you need to add a Label control to your document’s Library panel for this to work. I don’t know how you would be able to do that in Flex Builder.

Peter

3 Jeremy Hixon 06.12.09 at 11:45 am

The library you would be wanting is:

import mx.controls.Label;

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: