8.5. Making a User Input Field

Problem

You want to create a user input field to allow the user to enter text.

Solution

Set the text field’s type property to “input”. Alternatively, you can create and use a custom createInputTextField( ) method.

Discussion

When a text field is created using createTextField( ), it defaults to being a dynamic field. This means that it can be controlled with ActionScript but the user cannot input text into it. To enable the field for user input, set the type property to “input”, like so:

myTextField.type = "input";

Though it isn’t a requirement, input fields generally also have a border and a background; otherwise, the user may have difficulty finding and selecting the field so as to enter a value into it:

myTextField.border = true;
myTextField.background = true;

For a user to be able to input text, the field’s selectable property must be true, which is the default. You do not need to explicitly set the selectable property to true unless you previously set it to false.

As you can see, several steps are required to successfully make an input text field using ActionScript. While none of them are particularly difficult, they can be a bit tedious when you are creating multiple input text fields. Therefore, it is convenient to create a custom createInputTextField( ) method for the MovieClip class that takes care of these steps with a single method call. You should add the following code to a TextField.as file in your Flash Include directory for easy inclusion in ...

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.