Home / Articles / Adobe Flash / 10 Minutes with Flash: ActionScript You'll Never Have to Write Again

10 Minutes with Flash: ActionScript You'll Never Have to Write Again

Article Description

Robert Hoekman, Jr. is just as tired of writing code as you are, so he offers up some timely tips on storing ActionScript and loading it on-the-fly in this fourth installment of "10 Minutes with Flash."

Portable Scripting

If you've tapped into the world of ActionScript 2.0 and you're playing with code for the first time, you'll know exactly how overwhelming it can all become. Something as simple as loading an external asset into a movie is a decently complicated task for a code newbie. What you're about to do is write up the bulk of a script and put it somewhere safe so you can simple modify it for new projects.

So here's what you do:

  1. Open up your favorite ActionScript editor. You can use Notepad or TextEdit (I'm still waiting for Notepad Pro to come out), but Dreamweaver MX 2004 will really be the best tool around for this because it gives you color-coordinated code. If you're using Flash MX 2004 Professional, create a new ActionScript File to create the script there, as shown in Figure 2. (The comment about Notepad Pro was a joke. Don't bother looking for it.)

  2. Figure 2Figure 2 At last, you can write ActionScript in Flash, which makes entirely too much sense.

  3. Add the following code to instantiate an instance of the MovieClipLoader class (easier done than said in this case).

  4. var the_loader:MovieClipLoader = new MovieClipLoader ();

    You're using strict typing here to specify the data type of the MovieClipLoader and simply calling it the_loader because it's an easy name to remember.

  5. Next, create a listener object that will handle running methods for the_loader.

  6. var the_listener:Object = new Object ();

    The listener will be an Object object (redundancy is fun). Check Figure 3 to make sure you're doing everything right.

    Figure 3Figure 3 Caption: The beginnings of a real-life MovieClipLoader script.

  7. From here on, you'll simply call up the methods for the MovieClipLoader. You'll leave them empty for now because the idea is just to have a generic script you can modify and/or use in other projects.

  8. the_listener.onLoadStart = function (mc){
     // actions

    The onLoadStart method enables you to specify what, if anything, will occur when any load begins.

  9. Next up is the onLoadProgress method.

  10. the_listener.onLoadProgress = function (mc){
     // actions

    The best thing you can do with this event is trigger an animation for a preloader progress bar or something like that. This method gives up-to-the-millisecond feedback to the Flash Player, so you could use this script to advance a progress bar animation for every 1% of an asset that is loaded. I can't get into doing that right now, because I have only 10 minutes.

  11. Now create the onLoadInit method, which tells the Flash Player what to do when the loaded asset is fully initialized.

  12. the_listener.onLoadInit = function (mc){
     // actions
  13. The onLoadComplete is essentially used for the same purpose as the onLoadInit method—to tell the Flash Player what to do when a load is done, but it's less comprehensive. More info is available on the differences in the Help documentation in Flash.

  14. the_listener.onLoadComplete = function (mc){
     // actions

    By the way, the // marks indicate a comment in ActionScript, and any line beginning with two forward slashes will be grayed-out. When you modify this script, you can simply replace that line with any actions needed for that method.

  15. This last method will check for errors in the load.

  16. the_listener.onLoadError = function (mc){
     // actions
  17. Finally, you'll assign the listener object to the MovieClipLoader instance.

  18. the_loader.addListener (the_listener);

    When you're done, your script should match Figure 4.

    Figure 4Figure 4 What could be better than a completely generic script?

  19. Save this file as generic_mcl.as into a folder on your hard drive called MovieClipLoader (you need to create this folder now unless you psychically did it earlier).

3. Putting Your Hard Work to Good Use | Next Section Previous Section