User Guide
Creating text fields at runtime 223
However, you can’t use the variable name mytextVar to set the text field’s text property. You
have to use the instance name, as shown in the following code:
//This won’t work
mytextVar.text = "A text field variable is not an object reference";
//For input text field with instance name "myField", this will work
myField.text = "This sets the text property of the myField object";
In general, use the TextField.text property to control the contents of a text field, unless you’re
targeting a version of Flash Player that doesn’t support the TextField class. This will reduce the
chances of a variable name conflict, which could result in unexpected behavior at runtime.
Creating text fields at runtime
You can use the createTextField() method of the MovieClip class to create an empty text field
on the Stage at runtime. The new text field is attached to the Timeline of the movie clip that calls
the method. The
createTextField() method uses the following syntax:
movieClip.createTextField(instanceName, depth, x, y, width, height)
For example, the following code creates a 30 x 100-pixel text field named test_txt with a
location of (0,0) and a depth (z-order) of 10:
this.createTextField("test_txt", 10, 0, 0, 300, 100);
You use the instance name specified in the createTextField() call to access the methods and
properties of the TextField class. For example, the following code creates a new text field named
test_txt, and modifies its properties to make it a multiline, word-wrapping text field that
expands to fit inserted text. Then, it assigns some text to the text field’s
text property.
this.createTextField("test_txt", 10, 0, 0, 100, 50);
test_txt.multiline = true;
test_txt.wordWrap = true;
test_txt.autoSize = true;
test_txt.text = "Create new text fields with the MovieClip.createTextField
method.";
You can use the TextField.removeTextField() method to remove a text field created with
createTextField(). The removeTextField() method does not work on a text field placed by
the Timeline during authoring.
For more information, see
MovieClip.createTextField() and
TextField.removeTextField() in Flash ActionScript Language Reference.
Note: Some TextField properties, such as _rotation, are not available when you create text fields
at runtime. You can rotate only embedded fonts.