Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Make your own reusable classes using Flash and AS3

Make your own reusable classes using Flash and AS3

By Jody Hall | Published 11/22/2007 | Beginner | Rating:
Jody Hall
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.  

View all articles by Jody Hall
Classes, Packages, and the Classpath

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!




Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • Comment #1 (Posted by Eric - eric at surrealanimation.com)
    Rating
    Seems like there is an error in the tutorial. On page 3 when you mention the code "should" complete without errors the following error msg is encountered:
    1120: Access of undefined property myClip.
     
  • Comment #2 (Posted by an unknown user)
    Rating
    I also wrote:
    "In the fla file, create a MovieClip symbol. Give it an instance name of “myClip” and change the code to the following:"

    Creating the MovieClip, and giving it an instance name of "myClip" (in the fla) is up to you, and if you do it will run without errors.
     
  • Comment #3 (Posted by joe - jojoscafe01 at gmail.com)
    Rating
    I've converted my shape to a movie clip symbol, named it myClip in the .fla, and it still gives me the 1120 error. It shows the movie, but won't give me the output that we wanted. Is this a harmless error?
     
  • Comment #4 (Posted by an unknown user)
    Rating
    Drop a square convert to movieclip
    in lower left corner
    make sure instance name is same as movie clip name i.e myClip
     
  • Comment #5 (Posted by Alek Mlynek - alek at noisebug.com)
    Rating
    The reason people are having problems with this tutorial is this:

    var clipDragger:ClipDragger = new ClipDragger(myClip);

    The variable name should be different than the class name, since if you capitalize both by accident you will get this error.

    To fix:

    var myClipDragger:ClipDragger = new ClipDragger(myClip);

     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.