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

Please update flash player to view this Flash ActionScript tutorial!

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 {

// 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-2.fla

Flash Document Class Source Code MyClass2.as

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

Flash File directory

Document Class Directory

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

Declare Document Class

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.