An arbitrary number of anonymous loader loaders with separate data?
I have an arbitrary number of files that I need to load in an AIR application.
I want to iterate over an array of File object and create and run Loaders for each File.url file.
When they are done (COMPLETED event or IOErrorEvent.IO_ERROR), I want to write their data somewhere. If they fail, I want to make an exception report. I cannot find a way to reference the File object from an event listener (not IO_ERROR of course)
The best way would be to create separate event handlers for each Launcher that would record the "hard-coded" file information. At least I could use this function as an index in a dictionary:
foo (fileToLoad: File): void
{
var theCompletedHandler: Function =
{
var theFile: File = completedHandlerLookup [?????? this function ?????];
var theData: ImageData =
new ImageData (theFile.url, (_loader.content as Bitmap) .bitmapData);
// do something with the data
delete completedHandlerLookup [?????? this function ?????];
delete errorHandlerLookup.findByValue (theFile); // [sic]
}
var theErrorHandler: Function =
{
var theFile: File = errorHandlerLookup [?????? this function ?????];
// report the error
delete errorHandlerLookup [?????? this function ?????];
delete completedHandlerLookup.findByValue (theFile); // [sic]
}
completedHandlerLookup [theCompletedHandler] = theCompletedHandler;
errorHandlerLookup [theCompletedHandler] = theErrorHandler;
var theLoader: Loader = new Loader ();
theLoader.addEventListener (Event.COMPLETE, theCompletedHandler);
theLoader.addEventListener (IOErrorEvent.IO_ERROR, theErrorHandler);
theLoader.load (new URLRequest (fileToLoad.url));
Is it possible to use event.currentTaget and use a Loader instance as an index? Will this have some strange dependencies?
Greetings
a source to share
You can use event.currentTarget as an index lookup, if you clear all your links you should have no problem. Note, however, that loader event loaders are added to loader.contentLoaderInfo, so the loader will be available through e.currentTarget.loader.
It's also worth mentioning that a simpler approach to this situation would be to create a queue-based system, so it only downloaded one file at a time. For example, storing all files in an array. Then on COMPLETE or IO_ERROR you will process the current file and then run the next one. It would be slightly better, resource wise (only one bootloader), and avoid garbage collection problems.
I'm also a little curious as to why you are anonymous functions? They are not very good at garbage collection and actually complicate things. Is this a requirement?
Edit: Here's a rough (and incomplete) example of how a queue-based system would solve this problem.
var loader:Loader;
var queue:Array;
function addFile(file:File):void
{
queue.push(file);
}
function handleLoadedAsset(e:Event):void
{
var currentFile:File= queue[0];
//do whatever
stepQueueForward();
}
function handleFailedAssetLoad(e:IOErrorEvent):void
{
var currentFile:File = queue[0];
//clean up
stepQueueForward();
}
function stepQueueForward():void
{
queue.splice(0, 1);
var nextFile:File = queue[0];
loader.load(new URLRequest(nextFile.url));
}
a source to share