O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.9. Displaying Dynamic Text at Runtime

Problem

You want to display text within a movie.

Solution

Set the text property of a text field.

Discussion

Aside from being used as input fields, text fields are often used to display text to the user. Setting a text field’s text property causes the corresponding text to display in the field.

myTextField.text = "this will display in the field";

Tip

The Flash 5 technique of assigning a variable to a text field is still available, though not preferred. However, if exporting to Flash 5 format, you can assign a variable name to the text field using the variable property.

Special characters, such as \t for tab and \n for newline, can be used within a text string.

Displaying text programmatically in a movie is useful for many reasons. If a movie contains dynamic or frequently updated text, the values can be loaded from an external source, such as a database or XML document, and then programmatically displayed in text fields.

The trace( ) command displays text in Flash’s Output window, but the window is available during authoring only. You can use a text field to display output at runtime using a custom function:

function traceText (msg) { 
  myTextField.text += msg + newline; 
}
traceText ("Here's my message");

See Also

Recipe 8.1 and Recipe 8.2. See Recipe 8.10 for information on support for HTML-formatted text.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required