Introduction to the Document Class in ActionScript 3.0 – Tutorial
ActionScript programmers who are used to working in Flash have always been encouraged to keep code out of individual frames and movie clips as much as possible. In ActionScript 2.0, this usually meant either keeping function libraries in external files (and bringing them in using #include), or creating AS2 class files. Some designers still prefer to keep their code in the first frame of an .fla file, generally avoiding code anywhere else unless the situation is unavoidable. Whatever the case, it has always been good practice to keep code out of frames as much as possible – it helps you to stay organized and keep ‘all of your ducks in a row’, so to speak.
One great feature in Flash CS3 / AS3 which I find extremely helpful in this regard is the document class. With the document class, you no longer have to write any code on the main timeline for any reason. The document class becomes the “code center” for a given .swf file, and is the first code executed whenever that .swf plays. On top of that, you get to write pure object-oriented code in your class, which can work with all the other classes within the project scope, as well as exported items in your library or named instances sitting on the stage in your .fla.
Writing the MainClass.as File
Let’s imagine I am working with a brand-spankin’ new .fla file called “Main.fla”. The first thing we will do is create the actual document class file, which will of course be an “.as” file. You can create a new .as file by going to File->New and choosing a new ‘ActionScript File.’ Let’s call the file “MainClass.as”.
I usually put my document class files in the same directory as my main .fla. Since their names are very similar you will see them in your directory file listing (Mac Finder / Windows Explorer / Bash shell, etc) easily. (Being able to find files quickly is so so very important right?). Take a look at my Finder window:
Now I need to edit the MainClass.as file. Keep in mind that it needs to be a normal AS3 class file, including a specification of its package as well as class declaration. Here is an example of some simple code that will trace a string to the output window when we test the movie.
package {
import flash.display.MovieClip;
public class MainClass extends MovieClip {
public function MainClass() {
trace ('the main document class is ALIVE. Hooray! ');
}
}
}
Notice that we made it part of the default package, and that we imported the MovieClip class using (of course) the import statement. This is important (no pun intended), because, as you can see in the next line, our MainClass extends the MovieClip class. Document classes must either extend the MovieClip or Sprite class. If they are to use animations with frames, or need the methods and functions of movie clips, they will need to extend MovieClip. Otherwise, if you only need Sprite functionality, just use Sprite and save yourself some memory.
(For more on the MovieClip class you can read my article on Movie Clips in AS3, or read up in any of the new books on ActionScript 3 such as Essential ActionScript 3.0 by Colin Moock.)
Declaring Your Document Class in Your .fla
As explained by the Adobe documentation, the next thing you will want to do is declare the class name. You need to have all objects de-selected, and then you can click on the stage, open the properties window, and choose the name. We are going to call our class MainClass. Notice the first capital letter for the ‘M’, and second capital for the ‘C’. Also notice how there are no spaces in our class name. This is a convention, or a common practice, used for creating class names, in the Java / C++ world, and of course, it is now becoming a standard for AS3 developers.
I have created an .fla file called “Main.fla”, so I am going to call the main document class for that file “MainClass”. Notice of course, that we are setup in an ActionScript 3.0 document targeted for Flash Player 9. Don’t forget to press ‘Return’! ![]()
(Note: if you type in your class name, press return, and get a message telling you that a definition for this class path will be created, you need to make sure that you have a “MainClass.as” file saved so that Flash can find it. If you are getting this message, you must not have that file created.)
If you save both your .as file and your .fla file, you should now be able to test the movie, and viola! Your Main Class’ constructor will execute. You should see a message in your output window.
Further Ideas for Working with the Document Class
There is obviously a lot more to explore with the document class, but I hope you gained some knowledge from this tutorial that you can apply to your AS3 projects. Cheers! Jay
Tags: ActionScript & Flash, AS3, CS3, document class, Flash
![[image]](http://mowser.com/img?url=http%3A%2F%2Fwww.heaveninteractive.com%2Fimages%2Famazon%2F51Vo7OODj6L._SL160_.jpg)
Comments(5)

Thanks for the article, but I just thought you should know that your screengrabs don’t match your text.
For example …
1) You talk about the Finder Window, but have a screenshot of a ‘Site’ drop-down menu.
2) You mention the document class, but then have a screengrab of an FTP window.
Thanks for the tip Adrian!
I had an error in my article editor…. it should be fixed now.
Cheers
Jay
Great tutorial. I’m familiar with actionscript 2.0 and at the moment actionscript 3.0 is very new to me. I’ve spent all day trying to get to grips with it and I’ve been having a lot of problems. Just glancing at a few of your tutorials it seems you have a lot of articles which I wish I’d found this morning! I’ll be back here tomorrow! ha
I’m just learning ActionScript, but I am picking it up pretty quickly for being a newbee scripter. Thanks for spending the time to put this together- this is one of the best tutorials on what the hell a class path is….. everyone seems to have there own opinion (which I guess is fine!).
Thanks again
James
Hey,
What is the font on this page it looks fantastic!