DataLib

During my last jobs I wrote a queue loader class that stores all loaded files for future requests.

DataLib

Overview:

-Static

Since it’s a static class, any loaded file can be used from any parto of the app, even from external SWF files.

-Load queue

Any file must be add to a load queue passing the url and type of file to be loaded. The file can be added at the end of the queue or in a especific position.

DataLib.addItem("file.swf", DataLib.TYPE_SWF);
DataLib.addItemAt("file.png", 0, DataLib.TYPE_PNG);
DataLib.addItem("file.wav", DataLib.TYPE_BINARY);

Also is possible get the load queue.

var queue:Array = DataLib.queue;

To start loading you must call DataLib.load()
Also it’s possible to pause, resume and close the loading.

DataLib.pause();
DataLib.resume();
DataLib.close();

-Data storage

Every load file is stored for future requisitions. If necessary, files can be deleted freeing memory. Get loaded files using it’s URL.

var image:Bitmap = DataLib.lib[fileURL].data;
 
addChild(image);
 
DataLib.removeItem(fileURL);

-Custom Events

Custom Events cam be added to follow the loading. You can add an event in the class or in a especific item.

*Remember to remove all events from the class to prevent conflicts with other loadings.

DataLib.addItemAt("file.png", 0, DataLib.TYPE_PNG).addEventListener(DataLibEvent.PROGRESS, onItemProgress);
DataLib.lib["file.xml"].addEventListener(DataLibEvent.COMPLETE, onItemComplete);
 
DataLib.addEventListener(DataLibEvent.COMPLETE, onComplete);
 
DataLib.load();
 
function onItemProgress(e:Event):void{
	trace(Math.floor(e.bytesLoaded*100/e.bytesTotal)+"%");
}
function onItemComplete(e:Event):void{
	trace(e.data);
}
function onComplete(e:Event):void{
	addChild(DataLib.lib["file.png"].data);
}

-JPG, GIF, PNG, SWF, XML and BINARY

These are the supported files. You can get embed classes in SWF loaded files using the main movie application domain.

DataLib.addItem("asset.swf", DataLib.TYPE_SWF);
 
addChild(DataLib.lib["asset.swf"].data)
 
var EmbedClass:Class = ApplicationDomain.currentDomain.getDefinition("assetEmbedClass") as Class;
var instance:* = new EmbedClass();

You can get this and other classes using my google code.

http://andreanaya.googlecode.com/svn/trunk/

Theres also an example of usage of this class.

Thanks and I hope this could be usefull for you guys. Any problem or sugestion please tell me.

Anaya

This entry was posted in as3 and tagged , . Bookmark the permalink.

2 Responses to DataLib

  1. Elder Henrique Souza says:

    Muito, bom.
    Classe super útil, e muito bem estruturada! Continue o bom trabalho Anaya!

  2. Bruce Bannor says:

    Hey, When I try to do any sort of heavy loading, the dataLib just stops.. It prints out no errors, I’ve traced it somewhere between the loader.load call and the progress event. I had my listeners set to weak reference but I’ve changed that to no avail. So I thought possibly the GC was taking them. Any other ideas why this would fail to finish loading?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">