[image]

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:

The Finder Window with files

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.

declaring_document_class.png


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

When you create movie clips using the Flash authoring tool, export those clips in your library (details in this article). Now these clips will be available to manipulate via your document class, you just need to instantiate them and use the addChild method. You don’t have to have a constructor in your document class – you can just store functions there that you would like to call from other parts of your application. A good way to centralize code. Use a ‘main’ document class as the center point of your application, similar to a java main() method. (Advanced) Gather information about the user’s desktop system (flash player version, available screen width and height) OR other platform details (mobile phone camera availability, etc) in your document class. Then (if you are so inclined), instantiate a singleton or a global variable to store client details. Take advantage of this ‘point of first contact.

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

Jay Harley is a technology consultant and entrepreneur specializing in creating business productivity software. From 2002-2008, Jay worked as an MFA New Media instructor at the Academy of Art University in San Francisco, where he developed several courses in web application design and scripting, and enjoyed daily interaction with design students, offering guidance, support, and camaraderie as a media professional. Jay is now focused on providing Web 3.0 solutions to businesses in the form of collaboration and presentation software, by utilizing the power of the "Web as a Platform."



5 Comments so far

Adrian Parr on April 2nd, 2008

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.

Jay Harley on April 2nd, 2008

Thanks for the tip Adrian!

I had an error in my article editor…. it should be fixed now.

Cheers
Jay

Fin on April 27th, 2008

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

james on June 6th, 2008

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

Glen on August 29th, 2008

Hey,

What is the font on this page it looks fantastic!

Posting your comment.

Leave a Reply

You must be logged in to post a comment.


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser
Mobilytics