Archive for August, 2008|Monthly archive page

Loading with BulkLoader

If you’re like me, you find that loading assets into Flash makes you crazy. Face it: remembering several different APIs and then going through the rigors of daisy-chaining load operations together to acquire multiple assets (while doing distributive progress calculations) is a pain in the butt. Oh, and lets not forget error handling! After perfecting your load operation, it’s disheartening to realize that the whole application will hang if your second load fails.

So, wouldn’t it be great if there was a single API that could automatically handle loading all media types, AND manage multiple loads with cumulative progress reporting and a single complete event for the whole operation? I assume that’s what Arthur Debert was thinking when he came up with BulkLoader, which is an AS3 loading framework with all of the aforementioned capabilities, along with several other handy features including connection count settings, prioritization, and file weight calculations. The documentation is also very good! This is an innovation for Flash devs that rivals Galelio’s theories of astronomy; and like The Church’s response Galelio, BulkLoader is probably written off as dark science by Adobe who has never sprung for an all-inclusive loading API like this to include naively within Flash. So, check it out: BulkLoader is open source under the MIT license, and is ultra simple and convenient to use! First, download the library from: http://code.google.com/p/bulk-loader/

Setting up a BulkLoader works like this:

import br.com.stimuli.loading.BulkLoader;
var bulk:BulkLoader = BulkLoader.createUniqueNamedLoader();
bulk.addEventListener(BulkLoader.COMPLETE, this.onLoadComplete);
bulk.addEventListener(BulkLoader.PROGRESS, this.onLoadProgress);
bulk.add("someSWF.swf");
bulk.add("someImage.jpg");
bulk.add("someXML.xml");
bulk.start();
function onLoadProgress(evt:Event):void {
     var bulk:BulkLoader = evt.currentTarget as BulkLoader;
     // show progress... use bulk.percentComplete
}
function onLoadComplete(evt:Event):void {
     // get loader reference and clean up event listeners
     var bulk:BulkLoader = evt.currentTarget as BulkLoader;

bulk.removeEventListener(BulkLoader.COMPLETE, this.onLoadComplete);

bulk.removeEventListener(BulkLoader.PROGRESS, this.onLoadProgress);

     // acquire loaded assets from BulkLoader
     var mc:MovieClip = bulk.getMovieClip("someSWF.swf");
     var img:Bitmap = bulk.getBitmap("someImage.jpg");
     var xml:Bitmap = bulk.getXML("someXML.jpg");

     // purge asset references and clear loader from static registry to make it eligible for garbage collection
     bulk.removeAll();
     bulk.clear();
}

So, thanks to Arthur for this one! Awesome, convenient, and reliable! I had built a similar load package about a year ago for Lassie’s library manager, but I’ve since replaced the system with BulkLoader since it’s a substantially more robust system with nicer features than in my old light-weight version.

Lassie Migrates to AS3

I’ve been meaning to get a developer’s blog going for a while, so here it is. Finally!

Let’s start with some back story: I started tinkering with ActionScript 3 during Christmas break of 2007. I had a big game project coming up for the Peace Corps (now live at http://www.peacecorps.gov/game) which mandated that I get a firm grasp on AS3 before going into the new year. While I’d been tinkering with AS3 for a few months, I hadn’t gotten beyond sticking some MovieClip classes on stage before I was ready to tear my hair out in frustration. Every single thing I did upset the compiler and caused an error. It was like having a nightmare girlfriend: you could never make her happy no matter how hard you tried. Go the extra step to add a couple extra package and namespace declarations and then it’d start complaining about parenting and display accessibility. Ultimately, I found that a little failure went a long way toward sending me back into the safe and comfortable realm of AS2 — which glowed with the warmth of a cozy fire on a cold night. Maybe that’s a little poetic, but the imagery isn’t far off. Putting aside something that you’re good at to get into something new that you suck at is tough. AS3 is particularly difficult because it’s so much like AS2 but with so many little differences. There’s a lot of relearning required to take the plunge.

However, like so many times before, Lassie was the project that got me up to speed and pointed in a new direction. It’s a hobby project with no pressure or deadlines which helps dramatically; plus I can have fun with it! Of course, I had the same old frustrations off the starting blocks… errors, errors, errors. However, I hit my big AS3 epiphany when I realized how to read call stack errors. Then I was on fire.

So, if you’re new to AS3 then you’ve probably been frustrated by something like this before:

TypeError: Error #2007: Parameter child must be non-null.
   at flash.display::DisplayObjectContainer/addChild()
   at demo_fla::MainTimeline/init()
   at demo_fla::MainTimeline/demo_fla::frame1()

Oie. What does that mean? I used to read the first line, not understand it, get frustrated, and storm off back to AS2. Then I realized that this is a trace of the call stack, which lists the chain of events that led up to the problem… meaning that you should read it from the bottom. Suddenly it all made sense! In the above error, a frame1 action calls my init() method, which tries to add a child. However, the error tells me that a problem occurred while trying to call addChild(): “Parameter child must be non-null”. Translation: I was trying to add a null object as a child of the stage during my init() method… ohhhhhh.

Everything was easier after that. My programming took off. I was growing the Lassie game player quickly until min-January 2008 when I realized that I really needed a means to dynamically manage game data before moving forward, so I switched gears and have been building the game admin tool (Lassie Shepherd) ever since. Progress is slow, but a lot of that is because I’m starting from square-one in AS3 and have had to build all systems from the ground up; so I’ve had a lot of ground to cover!

However, it’s been a great move. I can’t say enough good things about the syntax, structure, and possibilities with AS3. While it was a tough transition, it was well worth it. I made my first real progress with AS3 a few days before Christmas 2007 when I got a Lassie room layout to render based on a static data structure. By the end of Christmas break I’d gotten the majority of the room application up and running. It was pretty cool to see it all coming together, and to see my code naturally deviate into better OOP form just based on the syntax and structure of AS3. It is truly a remarkable improvement over some of the derilect qualities of AS2.