In this section, we'll go over the basic ways to create and use all three types of text fields manually, and then we'll go over how to create a couple of them with ActionScript.
The basic way to manually create a text field is to select the Text tool from the toolbar (T on the keyboard) and click and draw the text fields on the stage.
After a text field has been created, you can still go back and resize it manually using the Text tool. You can also simply select it with the arrow tool, and handles will appear in each corner allowing you to scale it however you wish. You should not, however, use the Free Transform tool to adjust the size of a text field because it will actually stretch and resize the text inside the text field as well.
Static text, again, can only be manipulated in the authoring environment. You can, however, do some interesting things with it such as distribute each individual character to its own layer. This can be useful if you want to do some interesting animation with words because each object would have to be on its own layer.
To push letters from a single text field into individual layers, you will first want to create a text field by selecting the Text tool, setting the text type to Static Text, and clicking on the stage. After you have created the text field, you can simply type something in, like your name. When the text field is on the stage, and has some text in it, you can select it with the Selection tool, and either right-click (Maccontrol-click) and then select Break Apart, or you can choose Modify>Break Apart. After it has been broken apart, make sure all the text fields are still selected, and choose Modify>Timeline>Distribute to Layers (PCCtrl+Shift+D, MacShift+Open Apple+D). Now each letter has its own timeline named for that letter, as you can see in Figure 15.6.
Figure 15.6. After you select Distribute to Layers, each letter receives its own layer.
This technique can actually be done with all three types. When dynamic or input text fields are broken apart, they are turned into static text fields, but there isn't really anything special you can do with static text.
This type of text field is much more advanced than the static text field because it can be changed and manipulated at any point during authoring or runtime via ActionScript. As a matter of fact, you do not even need to type a single character in the text field itself to have text appear, as you will see in the first example.
For this first example, we are going to put text in a dynamic text field.
Now test the movie (Control>Test Movie) and you will see that the text is now in the text field. It does this by sharing a variable name in ActionScript and the Var property in the text field's properties. This was the way it was done way back in Flash 5. It still works, but now we are going to do it the correct way because text fields are now objects in ActionScript.
Now test the movie again, and you will see that the text field is displaying our text. This way of doing it is much easier to understand because instead of having a blind variable, you actually are setting text to a specific text field. This makes it much easier to go back and read. Because it is good practice to set text to the text field's text property, that is the way we will continue to do it.
So far, the two examples we have done using dynamic text were not very dynamic. We could have just written that text in right on the stage. In this next example, you will see why dynamic text fields are so important.
In this example, we are going to build a mouse tracker to show where the mouse is whenever the mouse moves.
Test the movie and you will see that whenever the mouse moves, the text fields will show its position on the main stage.
If at first nothing appears in the text fields, click somewhere on the stage. Sometimes the stage must receive focus before mouse events can be detected.
Now that you see how powerful and easy it is to update dynamic text fields, we are going to move on to our last text field type, input text fields.
As mentioned earlier, input text fields are exactly like dynamic text fields in how we can set text to them. But unlike dynamic text fields, the user can actually type text in input text fields to interact with our Flash document. Input text fields also have a few extra options that the other two text types do not. The Linetype option can be set to Multiline just like dynamic text fields, but it can also be set to Password, which would make any character in it appear to be an asterisk. That comes in handy when building login systems. Another option is the Maximum Characters field, which you can set to keep the length of what the user can input at a certain length or less.
You will begin to feel comfortable with input text fields as we go through the next example.
In this example, we are going to allow users to input information into one text field, and when they submit it, we will place that exact same string in another field capitalized.
Test the movie, type something in the input text field, and then click the submit button to see the same text displayed in our dynamic text field, but in uppercase format. Also, notice in our code that we get text out of text fields using the exact same property we use to set text to text fields, the text property.
So far, all we have done when we create a text field is to select the Text tool and draw the field on the stage. Now we are going to go through how to create one dynamically with ActionScript.