Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

ActionScript Document Class Save in Same Directory of Flash File

These are 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.

Please update flash player to view this Flash ActionScript tutorial!

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 {

// A Document Class file starts by declaring that
// 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. /////
//////////////////////////////////////////////////////////////////////

 

public function MyClass() { //////////////////////////////////////////
///// 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:

Flash Source File document-class-1.fla

Flash Document Class Source Code MyClass.as

Save the fla file and as file in the same directory as shown below:

document class and Flash File

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

Declare Document Class

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.