25
Aug/08
0

Queue loader items in an AS3 class

I wrote a class lately to queue loader items in AS3. I use it to load textures for my 3D engine but you can use it without modification for the loading of other files. The class is very simple, and doesn’t give any information about the loading progress apart from when it finishes loading all files. The class comes in handy when trying to load multiple files and have one event listener when all files are loaded. Below an example of the implementation:

[sourcecode language='actionscript']import qLoader; // You can change the package to for example com.input.qLoader;
var loader:qLoader = new qLoader();
// You can add relative file locations, but SWF’s online will likely
// only accept url’s on the same domain.
// You can activate debug traces by enabling the debug mode once the
// class has been created by uncommenting the following line:
// loader.mDebug = true;
loader.addLoad(”textures/stone.jpg”);
loader.addLoad(”textures/stone2.jpg”);
loader.addLoad(”textures/stone3.jpg”);
loader.addLoad(”textures/stone4.jpg”);
loader.addLoad(”textures/stone5.jpg”);
loader.addEventListener(”completeQ”,onLoaded);
// Initiate the loader
loader.initiate();

function onLoaded(evt:Event):void {
// In this example I create a bitmapData object
var bm:BitmapData = new BitmapData(loader.mFinishedItems["textures/stone.jpg"].width,loader.mFinishedItems["textures/stone.jpg"].height,false);
bm.draw(loader.mFinishedItems["textures/stone.jpg"],new Matrix());
}[/sourcecode]

The class needs to be an extension on the EventDispatcher class to work (otherwise it cannot dispatch events). I might update the class with progress info (like 100/4555Kb) or something and the loadNext() and loadItems() functions are the same but I’m too lazy for that now.

qLoader.as

Tagged as: , ,
Comments (0) Trackbacks (1)

Leave a comment