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!
