- Card-Carrying Code Hater (Sort of)
- Portable Scripting
- Putting Your Hard Work to Good Use
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:
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.)
Add the following code to instantiate an instance of the MovieClipLoader class (easier done than said in this case).
Next, create a listener object that will handle running methods for the_loader.
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.
Next up is the onLoadProgress method.
Now create the onLoadInit method, which tells the Flash Player what to do when the loaded asset is fully initialized.
The onLoadComplete is essentially used for the same purpose as the onLoadInit methodto 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.
This last method will check for errors in the load.
Finally, you'll assign the listener object to the MovieClipLoader instance.
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).
Figure 2 At last, you can write ActionScript in Flash, which makes entirely too much sense.
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.
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 3 Caption: The beginnings of a real-life MovieClipLoader script.
the_listener.onLoadStart = function (mc){ // actions }
The onLoadStart method enables you to specify what, if anything, will occur when any load begins.
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.
the_listener.onLoadInit = function (mc){ // actions }
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.
the_listener.onLoadError = function (mc){ // actions }
the_loader.addListener (the_listener);
When you're done, your script should match Figure 4.
Figure 4 What could be better than a completely generic script?