Using a Document Class

This page covers the concept of document classes, which are special class files attached to the main movie itself, rather than a subsidiary object. Document classes are very useful, and we'll apply them in many ways as we go.

A document class is in every respect identical to any other class file. There are no differences in form. I like to include the string "doc" in the name of any document class, so I can quickly recognize it among my file set; but this practice is not required.

Publish Settings Sub-Dialogue for Document Class

One thing makes a document class different from other classes, however: in the Flash interface, you establish a special relationship between the document class and the main movie. You do this through the Publish Settings dialogue.

First, be sure your main movie (.fla) is the active file. Select Publish Settings from the File menu. When the dialogue opens, click the Flash tab. Next click on the button labeled Settings. You should see the sub-window shown at left. In the box labeled "Document class," type the name of your document class (omitting the file extension .as, just as you do when setting a class linkage for a Movie Clip or other Symbol in the Library).

Click OK and you're all set: you can now use your document class for all sorts of interesting and useful purposes. Here at two common applications for document classes.


1. Root-level variables

Suppose you have a large number of Movie Clips in your project, and each one of those clips must be able independently to change a common value: for example, a stream of bullets of missiles fired at a series of invading spaceships, whose destruction increases the player's score.

If you attach the score variable to the bullet object, or to one of the invaders, you'll quickly discover that your score fluctuates strangely, or does not increase past 1. That's because variables attached to an instance of an object are confined to the object. To solve this problem, we need to define a variable at a level that is accessible to every object in the main movie -- a level we've previously defined as the root.

Any variable attached to the document class is by definiton at root level. You would access it from any subsidiary class (say, missileClass or invaderClass) using our new friend, MovieClip(root). Like so:

The statement above might occur within the class for a missile or bullet, within a hit test that detects collision with an invading ship. Note that MovieClip(root) is not followed by the name of a Movie Clip or other object (even though, technically speaking, the main movie is itself a Movie Clip). You never refer to the main movie by name -- or rather, you have already referred to it by invoking root. Notice also that a subsidiary class or object may alter the value of a variable declared in the document class, provided that variable is declared as public. This practice is permitted in ActionScript, though it is either discouraged or forbidden in some object-oriented programming languages, which allow changes in external variables only through a method defined on the higher-level object. (You don't need to worry about this right now.)

2. Spawning child objects

Another common use of document classes is to set up a main movie containing a large or variable number of component parts.

Consider our particle and raindrop demonstrations. Each involves several dozen instances of a Movie Clip. Creating these instances by hand would take hours, and would not be much fun. Happily, we can make them appear by writing just a few lines in the Constructor of our document class:

Several things happen in these few lines. First, we establish a loop that iterates 150 times. (The counter or index variable i records the number of iterations, but we don't use it in this particular example.) Inside the loop, we create a new variable called noob (the name is arbitrary) of type MovieClip -- which means that this variable is a container for Movie Clip objects. To this special variable, we assign the result of the expression:

This statement calls the Constructor method of a class called thingyClass, which must be defined in an ActionScript file named thingyClass.as. (This name is also arbitrary.) The Constructor of any class creates a new copy or instance of that class.

The loop above does our spawning. Think of it as a spawn loop. Note that we could have written it somewhat more compactly:

In the version above, we eliminate the container variable noob. The result is the same -- 150 instances of thingyClass, doing whatever they're scripted to do. In the terse version, however, we can't do anything to the new instances as they are created. The longer version allows us to do just that, as in this variation:

Here, each of our new instances appears at point 50 in the X axis of our stage.

Note that we do not need to invoke MovieClip(root) to set the x property of our new instance. We're already at the root... and that's always a fine place to be.




University of Baltimore Logo

Last updated: 09/29/09 16:45:53
Copyright © 2009 School of Information Arts and Technologies