AS3 BulkLoader example


I've been using AS3 BulkLoader for a while, which is great for loading multiple assets such as images and swfs into your application. Typical usage would look something like this:

instantiate BulkLoader object:

private var assetLoader:BulkLoader = new BulkLoader("instanceName");

add assets into the queue:

this.assetLoader.add("assets/myImage.jpg", {id:"key1"});
this.assetLoader.add("assets/myMovie.swf", {id:"key2"});

where id is the key, which is used to identify which asset to retrieve from the queue once it's been loaded.

To start loading assets you simply call the start() method:

this.assetLoader.start();

To get asset content, you would normally call getContent() method, which would return Bitmap for images or AVM1Movie for swf files. You could also call methods like getBitmap() and getAVM1Movie() to target specific file types.

The problem I encountered was that assets I was loading were encrypted. Therefore, I needed to read them in as ByteArray type and then decrypt them. Fortunately the latest version of BulkLoader has getBinary() method, but there is a trick to it. You need to tell BulkLoader to load assets in binary format when adding items into the queue:

this.assetLoader.add("assets/myImage.jpg", {id:"key1", type:BulkLoader.TYPE_BINARY});
this.assetLoader.add("assets/myMovie.swf", {id:"key2", type:BulkLoader.TYPE_BINARY});

And after all assets have been loaded, you get binary content:

var assetBinary1:ByteArray = this.assetLoader.getBinary("key1");
var assetBinary2:ByteArray = this.assetLoader.getBinary("key2");

Without specifying the type, BulkLoader will try to load data in text format which will result in the following error:

[BulkLoader] Failed to get content with url: 'file:///myFile.jpg'as type: [class ByteArray]

Setting the type to binary did the trick for me, so I thought I'd share it in case someone else falls into the same trap.

Oh, and I should thank Mark Lynch for helping me figure this out 🙂

Marko


This post was posted in , , , by on