8.2. Creating a Text Field

Problem

You want to create a text field at runtime that you can use in your Flash movie.

Solution

You can create a text field using the MovieClip.createTextField( ) method.

Discussion

You can create a text field at authoring time using Flash’s Text tool. Manual creation lets you see the layout of the objects on the Stage. However, many projects benefit from creating text fields dynamically. For example:

  • An animation with randomized text elements

  • A user interface in which items (and their labels) are created dynamically based on data loaded into the movie from a text file, XML document, or other source

  • A form that automatically adapts to a user’s input

  • Word games

The MovieClip.createTextField( ) method creates a text field at runtime. Note that the createTextField( ) method is invoked on a MovieClip object, not a text field. (If we had a text field already, we wouldn’t need the recipe, now would we?)

The createTextField( ) method takes six required parameters:

                  parentMovieClip.createTextField(name, depth, x, y, width, height);

where the parameters are as follows:

name

The instance name of the new text field

depth

The depth (within the MovieClip object) of the new text field

x

The x position relative to parentMovieClip’s registration point

y

The y position relative to parentMovieClip’s registration point

width

The width of the field in pixels

height

The height of the field in pixels

The new text field is created within the movie clip from which the method is invoked, ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.