MovieClip.createTextField( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
MovieClip.createTextField( ) Method Flash 6

creates a new TextField object
mc.createTextField(instanceName, depth, x, y, width, height)

Arguments

instanceName

A string specifying the instance name of the TextField object to create. The name must adhere to the rules for creating an identifier outlined under Section 15.5. To enable code hinting in the Flash authoring tool, use text field instance names that include the suffix "_txt", as in firstName_txt.

depth

An integer, between -16384 and 1048575 (inclusive), specifying the level on which to place the new text field inside mc. Higher depths obscure lower depths, so a depth of 6 is in front of 5, which is in front of 4, and so on. Depths above -1 are reserved for dynamically generated content. Therefore, in most cases, you should use a depth between 0 and 1048575 with createTextField( ). However, when creating dynamic content that you want to appear beneath all author-time content in mc (e.g., a run-time background), use depth -16384. Depths from -16383 to -1 (inclusive) are reserved for author-time content and shouldn't be used with createTextField( ). For important details, see Section 13.4.

x

A floating-point number specifying the horizontal position of the newly created field's left edge, in pixels, relative to the coordinate space of mc. See TextField._x.

y

A floating-point number specifying the vertical position of the newly created field's top edge, in pixels, relative to the coordinate space of mc. See TextField._y.

width

A floating-point number specifying the width of the newly created field's bounding box, in pixels. See TextField._width.

height

A floating-point number specifying the height of the newly created field's bounding box, in pixels. See TextField._height.

Description

The createTextField( ) method creates a new on-screen text field controlled by the ActionScript TextField object named instanceName. It is the runtime equivalent of creating a text field with the Text tool in the Flash authoring tool. If mc is the main movie, the new text field is placed at the point (x, y), relative to the top-left corner of the Stage. If mc is a movie clip instance, the new text field is placed at the point (x, y), relative to mc's registration point. The new text field is placed in mc's content stack at the specified depth, replacing any previous occupant of that depth. The text field's default type is "dynamic".

Once the text field has been created, it is controlled by the methods and properties of the TextField class. For instructions on deleting a text field, see the TextField.removeTextField( ) method.

Example

The following code creates a text field on the main timeline of a movie (_root), at depth 2, positioned at point (0, 0) (the top-left corner of the movie), with a bounding box width of 200 and height of 20. Once the field is created, the text "Hello world!" is displayed in it:

_root.createTextField("welcome_txt", 1, 0, 0, 200, 20); _root.welcome_txt.text = "Hello world!";

The following code creates the same text field, but also gives it a border and positions it at the center of the movie's Stage:

// Create the text field. _root.createTextField("welcome_txt", 1, 0, 0, 200, 20); // Add a border and some text. _root.welcome_txt.border = true; _root.welcome_txt.text = "Welcome to my site!"; // Make the border match the text size. _root.welcome_txt.autoSize = true;     // Set the alignment mode of the movie. Stage.align = "LT"; Stage.scaleMode = "noScale";     // Center the text field any time the movie changes size. _root.welcome_txt.onResize = function () {   this._x = (Stage.width/2) - (this._width/2);   this._y = (Stage.height/2) - (this._height/2); } // Make the text field listen for the Stage.onResize() event. Stage.addListener(_root.welcome_txt);     // Center the text field manually. This is required in the Standalone Player, // which does not automatically trigger onResize() when the movie loads. _root.welcome_txt.onResize();

See Also

MovieClip.createEmptyMovieClip( ), TextField.getDepth( ), TextField.removeTextField( ); the Stage class, the TextField class



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net