My interest in Flash started mostly because of a Jib-Jab cartoon ("This Land") in 2004. I'm the author of a feature I call "Mazoons," which are a combination of mazes and cartoons. In 2002, I even had a book published, "Super Silly Mazes." I'm not a professional programmer, but making my mazes interactive by programming them with Flash has become a hobby/obsession of mine in recent years. My efforts so far can be seen at http://www.mazoons.com/Flash_mazes.htm.
Let's suppose that in your programming experience, you know how to make a Movie Clip draggable with the mouse, and you also know how to write a routine to make a MovieClip respond to the arrow keys and move around the stage. Let's further suppose that you've done both of those tasks many times, and you get to thinking "Wouldn't it be great if I could just write one routine for each task, and then just apply it to future Movie Clips at will?" Enter reusable Classes!
The couple of classes that we'll write together are going to be really cool, but first let's set up a package to store our first classes, and set the classpath so that Flash can find them. This is something that I didn't understand for the longest time, but also something that becomes simple once you "get it."
Packages are just directories (or folders, if you prefer) on your hard drive. Inside the directories are stored one or more class files. The reasons you might want to store classes into separate packages are (1) mostly to avoid naming conflicts, and (2) to group similar classes together.
The key to understanding the whole package thing is to set up one folder on your hard drive as a "portal" directory. At least I think of it as being a portal, or an avenue, or a route, to the folders that lie beneath it. The location of it doesn't matter, it's entirely up to you, but this directory will be designated as being in the classpath. So, go ahead and create a new folder somewhere on your hard drive, and we'll tell Flash to add it to the classpath. I chose to call mine "Classes." Feel free to use that name, or substitute one of your own, because the name doesn't matter much, as it won't be used at all in the package statements of our classes, nor the import statements of our fla files.
There are two ways in Flash to designate a classpath. The first way is in the publish settings for the document, but this only works for one document at a time. I prefer the second way, which sets the classpath for all new documents. So, inside Flash, go to Edit, Preferences. In the Preferences dialog box, click "Actionscript" in the left pane. In the right pane, click the button that says "Actionscript 3.0 settings." In the Actionscript 3.0 Settings dialog box, click the button with the plus sign (+). Next, click the browse button (it looks like a target) so that you can browse your system to find the folder you just created. When you've located the folder, click OK on the browse dialog box. Finally, click OK on the Actionscript 3.0 dialog box, and the Preferences dialog box. Done!

Now that you have a classpath folder set, let's start off by just writing the most simple class file there could ever be. It will simply trace a message to the screen. Mainly, this will be a test to make sure we've got communication with our classes, but also we'll also refine the folder structure a little bit by creating subfolders beneath our "portal" folder to store our classes in. Don't worry; it will all become clear as we go along.
Inside of Flash, click the File menu, choose "New" and then
"Actionscript File." In the new Actionscript document's script pane, type the
following:
Wow! That was a lot of folder creation, and hopefully it doesn't seem too confusing. But here's the deal: Now your file's location matches the location spelled out in the package declaration. Note that the name of the "portal" directory is not included in this line, because that directory is flash's starting point, and that's exactly why I like to think of it as being a portal. It's the folder that flash already knows about, because we set it in the classpath, so all we need to be concerned about is organizing everything beneath that. The folders "com" and "mysite" will be the common denominators here (we'll always use them), but will remain empty, and the folders beneath "mysite" will be named according to the functionality of the classes inside them. Since we just wrote a TestClass, we just created a package for it called "testing," and we might use the "testing" package to store all the files we create where we test new things. Or you can feel free to delete it later. I just used it for an example.
Let's put the TestClass to the test and make sure it works.
Choose File, New, and create a new Flash File (Actionscript 3.0) document.
Click on the first frame of the timeline, and press F9 to go to the actions
panel. Type the following two lines:
[as]import com.mysite.testing.TestClass;
var testClass:TestClass = new TestClass();[/as]
Press CTRL-Enter to test the movie. You should immediately get a message traced to the output window saying “TestClass working.” That’s because that instruction was written into the public function with the same name as the class. This special function is known as the constructor, and it runs immediately one time whenever an object is created from the class with external code using the “new” keyword. So, the constructor function is a good place to put the code that you want to have run automatically whenever a new object is created from your class. The next class we’ll write will be a good example of this.
It's also interesting to note here that since you have your own global classpath folder set, it's not even necessary to save the fla file anywhere! The folder that was set gives Flash an additional place to look for classes, and also an additional place to look when we tell it to import something.
Now we're going to write our first truly useful class. Choose File, New, then Actionscript File. This class, when applied to a MovieClip, will make that MovieClip draggable with the mouse. This is a relatively simple thing to do using frame code in a fla file, and chances are you already know how to do it, but I chose it for an example because it is so simple, and we're just getting the hang of classes and adding complexity very gradually.
Type the following into the script window:Now click File, Save As. Navigate to the mysite folder and create a new folder called "mouse." Double click the mouse folder to enter it, and save the file as ClipDragger.as. Next, open a new Flash document. Let's test ClipDragger and see if we get our trace message this time:
[as]import com.mysite.mouse.ClipDragger;