[ Team LiB ] |
Recipe 8.2 Creating a Text Field8.2.1 ProblemYou want to create a text field at runtime that you can use in your Flash movie. 8.2.2 SolutionYou can create a text field using the MovieClip.createTextField( ) method. 8.2.3 DiscussionYou can create a text field at authoring time using Flash's Text tool. Manual creation lets you see the layout of the objects on the Stage. However, many projects benefit from creating text fields dynamically. For example:
The MovieClip.createTextField( ) method creates a text field at runtime. Note that the createTextField( ) method is invoked on a MovieClip object, not a text field. (If we had a text field already, we wouldn't need the recipe, now would we?) The createTextField( ) method takes six required parameters: parentMovieClip.createTextField(name, depth, x, y, width, height); where the parameters are as follows:
The new text field is created within the movie clip from which the method is invoked, which is called the parent clip or container clip. This example creates a new text field named myText—with a depth of 1, positioned at (0, 0), and with a width of 100 and a height of 20—within _root: _root.createTextField("myText", 1, 0, 0, 100, 20); Once the text field is created, it can be targeted using parentMovieClip.newTextFieldName, as follows: _root.myText._x = 100; The parent clip can be _root (the main timeline) or a movie clip created either at runtime or authoring time. You can use the duplicateMovieClip( ), attachMovie( ), and createEmptyMovieClip( ) methods of the MovieClip class to dynamically create a parent movie clip to hold the text field: _root.createEmptyMovieClip("myTextHolder", 1); _root.myTextHolder.createTextField("myText", 1, 0, 0, 100, 20); _root.myTextHolder.myText.text = "This is a new text object"; You can remove a TextField created with createTextField( ) simply by invoking the removeTextField( ) method on that object, as follows: myText.removeTextField( ); 8.2.4 See Also |
[ Team LiB ] |