Setting Text Color and Text Background Color

Setting Text Color and Text Background Color

Flash CS4 ActionScript 3.0 Tutorial

 

How to Change the Flash Text and Text Background Color with ActionScript

 

 

Input text and Dynamic text can be incorporated into ActionScript. You can write code to alter the text color or background color of either type of text. For more information about other text subjects, see topics, Dynamic Text, Input Text, Inputting Values and Displaying Results, and Setting Text Font and Size.

In this example we will use an Input text field to demonstrate the process.

Tutorial Elements

Input Text box with instance name of inText_txt. Enter the word Flash in the text box.

 

Step One

In a new Flash document, add a new layer. Name the upper layer Actions and the lower layer Content.

Step Two

Click the keyframe for the Content layer.

Step Three

Add an empty Input text block. Choose Input text In the Properties panel.

Step Four

Double-click the text box and type the word Flash in the text box.

Step Five

Click the Input text box and assign it an instance name of inText_txt in the Properties panel.

Step Six

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

Step Seven

Copy/Paste the following code to the ActionScript panel.

var changeText:TextFormat = new TextFormat();

changeText.color = 0xFF0000;

inText_txt.setTextFormat(changeText);

inText_txt.background = true;

inText_txt.backgroundColor = 0xEEEEEE;

inText_txt.border = true;

inText_txt.borderColor = 0xAAAAAA;

Step Eight

Click the Auto Format button to format the code

Step Nine

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The text, background, and border have been altered by the ActionScript.

Dissecting the Code

The first line adds a new TextFormat variable named changeText.

The next line sets the text color to FF0000. Since the first two characters control the red value, the new color will be a bright red. The 0x in the beginning lets ActionScript know that it is a hexadecimal color value.

Line three assigns the changeText variable value to the Input text. In other words, show the word Flash in red.

The next two lines make sure the text object background is turned on and sets the background color to EEEEEE.

Lines six and seven turn the text box border on and set the color to AAAAAA.

 

Shutterfly 50 Free Prints 160x600