Question

While my code is pretty big and fragmented over many files to show here, i basically boils down to this:

I have a function which does this:

var loadedScript:Loader;
function load():void{
    loadedScript = new Loader();
    // loadedScript is initilized somewhere and other stuff
    loadedScript.loadBytes(bytes, context);
    loadedScript.contentLoaderInfo.addEventListener(Event.COMPLETE, scriptLoaded, false, 0, true);
}

when i call this function 2 times in a row in such a way that its called the 2. time before loadedScript.loadBytes(bytes, context); from the 1. time can finish, then the "scriptLoaded" method is called only for the 2. time, not the 1. time

So, is this intentional behaivor from the loadedScript.loadBytes(bytes, context); method, or a bug, cann i bypass this somehow?

Was it helpful?

Solution

you could ofcourse create a loader-queue, something like this:

var queue:Array = new Array();
function load(bytesToLoad:*):void {
    queue.push(bytesToLoad);
    processQueue();
}

function processQueue():void {
    //check if items are on queue
    if (queue.length > 0) {
        //create a loader
        var ldr:Loader = new Loader();
        //add event for loading complete
        ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, scriptLoaded, false, 0, true);
        //load the bytes (get first item from the queue, which contains the bytes to load)
        ldr.loadBytes(queue.shift(), context);
    }
}

function scriptLoaded():void {
     //do stuff

     //process next item in queue
     processQueue();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top