Document Class Save in Same Directory of Flash File
This is extend tutorials of the first two series of Flash ActionScript 3 tutorials. We will also print out the text "Hello World" on the stage of our Flash movie. In this tutorial series, we will use Document Class to print out the text "Hello World". We will re-use the ActionScript codes that we used before.Flash Tutorial Content:
This Flash ActionScript 3 tutorial also learn how to print out the "Hello World" on the stage of flash movie. This time we use the Document Class to print out the text "Hello World" with ActionScript 3.
What is a Flash ActionScript Document Class? You can think a Document Class as an external file which allow us to write Flash ActionScripts on it. In other words, the ActionScripts are not write on the main timeline of Flash Movie that we used to do so in previous tutorials.
The Document Class file can be created with Flash CS3 application, a good Text Editor, or other ActionScript softwares. The file extension of Document Class is .as (for example, MyClass.as, FlashWonderLand.as, MainClass.as, etc...).
The Document Class will be executed first when the Flash Movie play. The Document Class is sometimes saved in the same directory of your Flash Movie.
Flash ActionScript Codes:
package {
// it is a package that contains a class
////////////////////////////////////////////////////////////////////
///// Define what are needed in the program /////
///// This is usually done by using import /////
///////////////////////////////////////////////////////////////////
// We need to display the TextField object on the stage
import flash.display.MovieClip;
// We need to use the flash.text classes to display text to TextField
import flash.text.*;
//////////////////////////////////////////////////////////////////////
///// Define the Class now. /////
///// This is a public class so that the Flash Movie can use it. /////
///// The class name is MyClass. /////
///// The class name MUST be same as the ActionScript File name /////
/////////////////////////////////////////////////////////////////////
public class MyClass extends MovieClip {
//////////////////////////////////////////////////////////////////////
///// This class contain a function. /////
///// The function name is also MyClass. /////
///// If the function name is same as the class name, /////
///// the function will be executed when the class is initiated. /////
//////////////////////////////////////////////////////////////////////
///// The codes below is exactly the /////
///// same as previous tutorial /////
//////////////////////////////////////////
// Add Content to Text Field
textField_txt.text="Hello World";
// Autosize TextField with the text and align to CENTER.
textField_txt.autoSize=TextFieldAutoSize.CENTER;
///////////////////////////////////////////
///// Align TextField to Stage Center /////
///////////////////////////////////////////
// Find the center co-ordinate of Stage
var stageCenter_x:Number=stage.stageWidth / 2;
var stageCenter_y:Number=stage.stageHeight / 2;
// Find the center co-ordinate of TextField
var textCenter_x:Number=textField_txt.width / 2;
var textCenter_y:Number=textField_txt.height / 2;
// Align TextField to Center
// Remember that textField_txt.x and textField_txt.y
// is the Top Left corner of TextField
textField_txt.x=stageCenter_x - textCenter_x;
textField_txt.y=stageCenter_y - textCenter_y;
}
}
}
Download Flash Source File:
Save the fla file and as file in the same directory as shown below:

Delcare your Document Class in the Properties window as shown below:

Remarks:
You should notice that most Document Class downloaded from the Internet is usually saved in a different directory, for example, com/flashwonderland/tweens. The next tutorial will show how to use the Document Class if save in a different directory of the Flash File.