moock.org is supported in part by


June 14, 2007

Chapter 27, Paragraphs 1-9, Essential ActionScript 3.0

Here are the first 9 paragraphs of Chapter 27 of Essential ActionScript 3.0

27. Text Display and Input

Flash Player offers an extensive, sophisticated API for working with text. In this chapter we’ll look at some of its key features: creating and displaying text, formatting text, and handling text input.

The information presented in this chapter applies specifically to Flash Player (both the browser add-on and standalone versions), but is also generally applicable to any Flash runtime that supports full-featured text display and input, such as Adobe AIR. Note, however, that unlike Flash Player, Adobe AIR provides full-featured HTML and CSS support, analogous to that found in Web browsers such as Microsoft Internet Explorer and Mozilla Firefox. When working with other Flash runtimes, be sure to consult the appropriate documentation for information on text support.

The centerpiece of Flash Player’s text API is the TextField class, which provides control over text displayed on screen.

In this book (and in most ActionScript documentation), the term “text field,” refers, in a general sense, to a given text field on screen and its corresponding ActionScript TextField instance. Meanwhile, the phrase “a TextField object” refers more specifically to the ActionScript object that controls a text field.

Creating and Displaying Text

To display text with ActionScript, we first create a TextField object. The TextField object represents a rectangular text container that can be displayed on screen and filled with formatted text via code or user input. For example, the following code creates a TextField object and assigns it to the variable t:

var t:TextField = new TextField();

After creating a TextField object, we use the TextField class’s instance variable text to specify the text to display. For example, the following code specifies “Hello world” as the text to display in t:

t.text = "Hello world";

Finally, to display the text field on screen, we pass the TextField object to the addChild() or addChildAt() method of any DisplayObjectContainer that is currently on the display list. For example, assuming someContainer is on the display list, the following code causes the text field t to appear on screen:

someContainer.addChild(t);

Example 27-1 shows the preceding code in the context of a demonstration class, HelloWorld. In the example, notice that we import the TextField class (along with all classes in the flash.text package) before using it.

Example 27-1. Displaying text

package {
  import flash.display.*;  
  import flash.text.*;  // Import TextField
  public class HelloWorld extends Sprite {
    public function HelloWorld () {
      // Create a TextField object
      var t:TextField = new TextField();
      // Specify the text to display
      t.text = "Hello world";
      // Add the TextField object to the display list
      addChild(t);
    }
  }
}
Posted by moock at June 14, 2007 08:00 PM