Now we have a MovieClip in the library of the fla that's linked to a class. Not only that, but we can drag out as many instances of this class as we want, and they'll all be draggable. Furthermore, we can create instances programmatically and add them to the stage programmatically too. Whether you realized it or not, you could even write a simple "for" loop and programmatically create, say, 10, 20, or even 500 instances of your class and add them to the stage. Delete the code on frame 1 of the fla file and enter this instead:
import com.mysite.mouse.ClipDragger2;
for (var i:int = 0; i < 20; i++) {
    var cd2:ClipDragger2 = new ClipDragger2();
    addChild(cd2);
}

Test the movie. This creates 20 instances of ClipDragger2 and stacks them atop one another. You can confirm that there's more than one if you start dragging them around. While making too many copies of this clip might be a frightful waste of memory and bad practice, it just might be starting to spark some cool ideas for you, as well.

But back to the topic at hand. Delete that fun code and let's get back to business. Let's suppose that you're ecstatic about being able to create draggable clips just by linking them to this class. Draw a blue square on the stage, select it, and press F8 to convert it to a symbol. Next, you attempt to enter the same information as before, to link your symbol to the custom class:



Only this time, when you click the OK button, you get a message from Flash that looks like this:



So, it seems to be that the rule is this: Each library symbol can only have one custom class linked to it. What to do, what to do? It turns out that in this case, the Base Class field is going to come in quite handy. Change the two fields so that they look like this instead:



This time, when you click the OK button, Flash replies with this:



Click OK. What this dialog means is that flash couldn't find any class called "Square" in the classpath. It further explains that a class with that name will be automatically created for us at compile time, and by clicking OK we accept this generous offer. I recommend you never check "Don't warn me again," as this dialog box is really a great indicator of whether or not Flash finds a class of the same name in the classpath. If you don't get this warning, better choose a different name!

Really, what's happening here is that by having both Class and Base Class fields, we are given the opportunity to use yet another level of inheritance. That way, we can link more than one symbol to the same class, by just using the Base Class field instead, and just giving the Class field some more generic name, and letting flash create the Class.

In this case, Flash has made a class for us called "Square." The base class of Square is our custom class (long name com.mysite.mouse.ClipDragger2). And since our custom class extended MovieClip, the base class of that is flash.display.MovieClip. Can you see the chain of inheritance here? If not, think about it some more:

Our class extends MovieClip (this is written in the class file itself)
The Base Class extends our class (Base Class field of dialog box)
The Square class extends the Base Class (flash makes a Square class if you don't define one elsewhere).

Now, let's go back and change the linkage of the Circle clip, too, changing the base class to our custom class and letting Flash create a "Circle" class for us! This has the further advantage that we no longer need to import anything in the fla file's code. We can make new instances of the Circle and Square by just writing:
var circle:Circle = new Circle();
var square:Square = new Square();
addChild(circle);
addChild(square);

It should be obvious why we no longer need to import anything. Now we aren't dealing with ClipDragger2 directly anymore. Now we're dealing with classes called Circle and Square, and they are in turn are based on ClipDragger2. Flash has no trouble finding the Circle and Square classes, because it creates them for us. And the information about where to find the ClipDragger2 class was entered in the dialog box. Clear?

Obviously, you could define Circle and Square classes yourself instead of having Flash create them for you, and still use ClipDragger 2 as the base class. But in that case, you might as well do everything manually, and just have the Circle and Square classes extend ClipDragger2, and give them empty constructors. Why do all that, when Flash will do it for you? It's too much work! (If anyone knows a good reason, be sure and let me know. I can't think of one at the moment).

Once again, I hope this tutorial has sparked your imagination. If you enjoyed it, won't you write and tell me so? Or, just post a comment here. Thanks!

Jody Hall (Mazoonist)