A Textfield is another display class, like MovieClip, Sprite, Bitmap and Shape.
Here is the link to the page on the Display Object Class in your help-site: http://soma.sbcc.edu/users/ajarabo/Flash_II/11_display_class.html
A movieclip is a type of a Sprite that contains a timeline (a movieclip symbol with one frame in the timeline could be programmed as a Sprite).
Movieclips and sprites belong to the DisplayObjectContainer class but a Textfield is directly a sub-class of the InteractiveObject class.
There are 3 classes of textfields in Flash:
1. Static Text: As a class is not part of the Interactive object. |
myText . type=TextFieldType . STATIC | |
2. Dynamic TextField: Those are textfields that can be used as static text or can return the values of variables in the code, like a window that shows the current score of a game. | myText . type=TextFieldType . DYNAMIC | |
3. Input TextField: This is a textfield that is still active in the swf format, so the user can write on it working as form input. | myText . type=TextFieldType . INPUT |
To create a textfield through code, like with any other type of class, you need to create a new instance of the class,
|
Textfields have a number of properties that can be set through code to define their characteristics and appearance. Check all of then in the help page below.
Link to the page on TextFields in your help site: http://soma.sbcc.edu/users/ajarabo/Flash_II/07_textField.html
There are 13 properties of a TextField that can only be declared through a TextFormat object, by creating a new instance of the class.
Once the object has been created you can apply it to a number or all the textfields in the project.
A TextFormat object allows you to choose design properties. To apply it to the TextField you will have to do a work around: you need to assign the TextFormat object (already named it through the TextFormat variable) to a property of your TextField. That property will change depending on when you do this:
1. To apply the settings BEFORE you add the text content, use the defaultTextFormat property of the TextField
2. To add the formatting AFTER you add the text content, use the setTextFormat property of the TextField
To define the actual content of the TextField you have to use the text property of the TextField (myText. text = "Tell me what name do you want to use today")
See the code of a textfield created through code that uses the TextFormat object:
var myText : TextField = new TextField() myText . defaultTextFormat = sectionText myText. text = "Tell me what name do you want to use today" addChild (myText); |
TextFormat parameters:
|
WORKING WITH EXTERNAL ACTIONSCRIPT FILES
In the Flash I class we always work in the Flash authoring environment, attaching the ActionScript code to keyframes in the Timeline. We also build complex movieclips that contain both complex content and complex code.
Programmers are usually not familiar or comfortable in the authoring environment so the prefer to work with files with extremely simple timeline and graphic elements placed in the library (with linkage to ActionScripting). They build all the ActionScripting from an external text file, called a class file (name.as).
Visit the link to information on a class file in your help site: http://soma.sbcc.edu/users/ajarabo/Flash_II/01_class_file.html
Help video in you tube from Nils Milahn on class files, part 1: http://www.youtube.com/watch?v=VIQrEGm8kB8&list=UUmmOCkEOwlwmp4vHsIaULWg&index=11&noredirect=1
Part 2 of the video:http://www.youtube.com/watch?v=eMRngAk3TS4
The class file should have the same name that the fla and the swf project (drag_game.fla and drag_game.swf, drag_game.as)
They should also be placed (.swf and .as files) in the same folder in the the root folder and the server.
There are small differences in coding when you build the code from an external class file. The main ones are:
1. Defining the package and import classes at the beginning of the file. You might have seen that when you type code in the ActionScripting window flash adds some lines at the top importing specific parts of the code, like:
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.MouseEvent;
When using an external ActionScripting file you will begin by creating a package That begins by calling all the "parts of the code needed". Then (inside the package) you will create the new class that holds your code.
2. Public and private classes.
The class body appears within the class declaration's curly braces, and it consists of properties and methods. Properties are variables associated with the class, and you declare them much as you would declare variables by using the var keyword. However, properties also must be modified by attributes, which determine the scope of the property. The following is a list of the attributes you can use with properties:
private __
Properties are private when they're accessible only within the class.
public __Properties are public when they're accessible within the class as well as from instances of the class.
protected __Properties are protected when they're accessible only within the class and to subclasses.
internal __Properties are internal when they're accessible within the package.
The default state of a property is internal unless you specify a different attribute. In most cases, properties should be declared as either private or protected. By convention, it's helpful to start private and protected property names with an underscore (_). The following declares a new private property called _id within the Example class:
package {
public class Example {
private var _id:String;
}
}
*In ActionScript 3.0 all ActionScript code must appear within a class. All classes must be placed in packages. A package is a way of organizing classes into groups, and in ActionScript 3.0 a package is synonymous with a directory on the filesystem.
Packages are relative to the classpath, for this initial discussion the classpath is defined as a path relative to the project (the .fla file, in the case of Flash or the main class or MXML document in the case of Flex). Therefore, the top-level package is synonymous with the project's root. The package declaration is always the first thing that appears in a class file.
The name of a class starts with a capital letter by convention. The name of a class must follow the same naming rules as variables and functions (consists of letters, numbers, and underscores, and cannot start with a number). The class declaration appears within the package declaration.
3. Defining the Constructor function of the Class
This is the first function of the class that runs immediately as the file gets initialized and hosts important information.
It should have the same name that the class and the fla name.
Every class can have a special method with the same name as the class itself. This special method is called the constructor, and as the name implies, you can use the function to construct new instances of the class. In ActionScript 3.0, all constructors must be public. Unlike standard methods, constructors cannot return values, and they must not declare a return type. The following declares a constructor method for the Example class: