Creating a Text Box with Code

Creating a Text Box with Code

Flash CS4 ActionScript 3.0 Tutorial

 

How to Add a Text Box with Flash ActionScript

 

 

In addition to creating text boxes with the text tool, you can add a text object using code. The object is added as a text object and assigned parameters such as font, size, and color. The text object can then be placed on the stage with ActionScript. In this example you create a TextField object and a TextFormat object.

 

Step One

Open a new document and change the layer name to Actions

Step Two

Option double-click (PC - Alt double click) the first keyframe of the Actions layer to open the ActionScript panel.

Step Three

Copy/Paste the following code in the ActionScript Panel:

var textObj_txt:TextField = new TextField();

var textStyle:TextFormat = new TextFormat();

textObj_txt.text="Learning Flash the Easy Way";

textObj_txt.width=400;

textObj_txt.height=140;

textObj_txt.wordWrap=true;

textObj_txt.border=true;

textObj_txt.borderColor=0xCCCCCC;

textObj_txt.background=true;

textObj_txt.backgroundColor=0xE1E1E1;

textObj_txt.x=60;

textObj_txt.y=50;

textStyle.align="center";

textStyle.bold=true;

textStyle.color=0x00CC66;

textStyle.font="Verdana";

textStyle.size=48;

textObj_txt.setTextFormat(textStyle);

stage.addChild(textObj_txt);

Step Four

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). Your new Text object appears on the stage.

Dissecting the Code

The first line establishes a new TextField object and assigns it a name, textObj_txt. The next line creates a new TextFormat object with the name textStyle. The next line defines the text to be displayed.

The large block of code sets the values for the TextField object. The text box is 400 pixels wide and 140 pixels high. Word wrap is turned on and it has a gray border. The box has a medium gray background and is located 60 pixels in the x direction and 50 pixels in the y direction.

The next block sets the text style. The text is aligned to the center and is bold. It is green, 48 pixel, verdana text.

To apply the format, the setTextFormat() method is used. The addChild line adds the text box to the stage.

 

Royalty-Free Stock Video at Pond5