Document Class Save in Different Directory of Flash File
In this tutorial we will also use Document Class to print out the text "Hello World". However the Document Class file (.as) is saved in a different directory of the Flash File.Flash Tutorial Content:
You should notice that most Document Class downloaded from the Internet is usually saved in a different directory, for example, com/flashwonderland/tweens. This tutorial will show how to use the Document Class if save in a different directory of the Flash File.
Flash ActionScript Codes:
package com.flashwonderland {
// 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:

Conclusion:
Now we can use three methods to print out the "Hello World".
1. Draw a textfield on the stage and use ActionScript on the main timeline to print out the text.
2. Use ActionScript on the main timeline to create a new textfield and print out the text.
3. Draw a textfield on the stage and use Document Class to print out the text.