views:

143

answers:

0

I have a Flash Loader component dynamically loading images into a movieclip. Since it may take longer than the clips intro animation to load the image, I have the clip stop right before the image is needed (and display a loading animation), and then when the image is loaded play the clip again. There's also logic to play the whole clip without stopping in the event the image is loaded before it's needed (rendering the callback useless).

This is the code:

img_loader._visible = false;
img_loader.autoLoad = false;
img_progress.percentComplete = 0;
img_progress._visible = false;

//Create listener object.
if (!loaderListener) {
    img_progress.mode = "event";
    img_progress.source = img_loader;

    var loaderListener:Object = new Object();

    loaderListener.complete = function(eventObject) {
        trace("Complete "+img_loader.percentLoaded);
        trace(img_loader._width);
        play();
    };

    loaderListener.progress = function(eventObject) {
        trace("Progress");
        trace("Done "+img_loader.percentLoaded);
    };

    //Add Listener.
    img_loader.addEventListener("complete",loaderListener);
    img_loader.addEventListener("progress",loaderListener);
}

if (this.images[0]) {
    img_progress._visible = true;
    trace("loading "+this.images[0]);
    img_loader.contentPath = this.images[0];
    img_loader.load();  
} else {
    stop();
}

This works, but rarely. It seems to be dependent on the loading time. What the output is supposed to look like is this:

loading http://.../projects/toyota_dealership.jpg
Progress
Done 1.99702474868142
0
Progress
Done 1.99702474868142
0
Progress
Done 5.26980119911644
0
.
.
.
Progress
Done 84.9073614930352
0
Progress
Done 91.4529143939052
0
Progress
Done 92.5438398773836
0
Progress
Done 100
841
Complete 100
837.1
100

However what I often get is this:

loading http://.../images/projects/fennel_mall.jpg
Complete 0
0

The complete callback fires right when the load method is called. Means I have no way of knowing when the image is actually loaded or how far along it is. Why could this happen? Exact same domain, exact same DSL connection, exact same SWF and time period. Can happen with the same image too. Any guidance?