Understanding the The Document Class in Flash
2When you start thinking that you need more coding than you can do in flash’s timeline and in your FLA, you will then need to use the document class. This class means you don’t need any code on the timeline, and all your codes go into using external classes.
Related posts:
- Create Your First Android AIR App in Flash
- How to Create a Snake Game in Flash Using AS3
- Creating Reflection in Flash Using AS3
- Bitmap Data Manipulation in Flash ActionScript 3
- Creating and Controlling Flash Animation with XML
- How to Build a XML Driven Quiz Application in Flash
- Approach to Understand XML and Flash Workflow
Let’s start with a basic document class.
The first line defines the package, which is a mandatory structure that tells the compiler about your class. You can think of the package as describing the path to your action script file. We will be keeping all the files in the same folder in this example.
Next are the important statements that you will use in your package; you must import any flash classes that you are using in your class. In our case we are importing the MovieClip package from flash, because we are using the MovieClip instance in our class. When a class extends another class, the new class has all the capabilities are added the original parent class.
The key word is Main (the name of the file is called Main.as) so here the keyword makes the Main class a descendent, or child, of the MovieClip class. This allows instances of the Main class to do all the things that can be done with movie clips.
To make the MovieClip class available to the new class Main, you need to write a code that imports the MovieClip call into your file.
You either use MovieClip or Sprite (Sprite is a MovieClip without a timeline).
The main class must extend this class (MovieClip in our example) and it becomes the whole class. In our example we will use the Sprite class.
Inside the main class is a constructor function, this is the main function of the class. This function must be public as well. If you choose to have other functions they would be here:
Although a class will often contain many functions, each class must have one function that has the same name as the class file. This class is the constructor function, and since it is mandatory, it is automatically created when you make a new actionscript file. The constructor function is automatically called every time an instance of the class is created.
The constructor class must be public. The term “public” is known as an access modifier. By setting the class to public, you are saying that this class can be accessed from any other file. Other access modifiers are:
Private – Methods and properties that are labeled private are available only from within that class file.
Protected – Methods and properties that are labeled protected are available only from within the class file and descendants.
Internal – Methods and properties that are labeled Internal are available only from within the class file and to all other files within the same package.
Like it was said earlier, a class will have many functions, but since ours is a simple example we will put all our code in the constructor.
Fist, create a flash actionscript 3 file and call it “test” or whatever you like.
Now create an ActionScript file and call it Main.
In the ActionScript file, type the following (you can use MovieClip if you like, I am using the Sprite class.)
This will create a circle and traces “circle added to the display list”.
Now that a class has been created we must tell flash where it is. In the Properties panel:
In the properties section write the name of the class and its location.
Where is says “class” you will type in Main, as this is what our AS 3 file is called.
Now when you run this, you will see a circle in the swf, and in the output section of flash it will write circle added to the display list!
The final output will look like this:
That is it for this tutorial, happy flashing!
[amember_protect guests_only]If you are already a Premium Member then just sign in and you can download source file for this tutorial.
Not a member? Sign up today or read more about our Premium Member area.
[/amember_protect]
[amember_protect user_action=’hide’ visitor_action=’hide’]
Download the source for this tutorial by clicking the button below:
[/amember_protect]
Very good example of class in flash.Thanks for the code. It really helps us to understand the importance of class in flash.
Hi, please can you shortly tell me how to work with multiple classes,
if I create multiple classes for multiple sprites, how to use Document Class?
What data needs DocClass to contain?
I hope you understand me.