There's no need to make a MovieClipLoader instance for each asset you want to
load.
Create one MovieClipLoader instance (as a property of the class) and make the
class itself the listener.
And rather than passing a function to the class which is to be called when all
loading is done,
dispatch an Event.
// not tested
import mx.events.EventDispatcher;
class mc.LoadChecker {
private static var dispatcherInit =
EventDispatcher.initialize(mc.LoadChecker.prototype);
public var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
public var itemsToLoad:Array;
private var itemsLoaded:Array;
private var itemsLoader:MovieClipLoader;
public function LoadChecker(a:Array) {
trace("loadchecker! created:"+a.toString());
itemsToLoad = a;
}
private function init():Void {
trace("LoadChecker ::: init");
itemsLoader = new MovieClipLoader();
itemsLoader.addListener(this);
loadItems();
}
private function loadItems():Void {
trace("LoadChecker ::: loadItems");
itemsLoaded = new Array();
var len:Number = itemsToLoad.length;
var tmpLoad:Object;
for (var i=0; i<len; i++) {
tmpLoad = itemsToLoad[i];
//array contains custom objects that contain url and mc vars
var url:String = tmpLoad.targURL
var mc:MovieClip = tmpLoad.targMC;
var holder:MovieClip = mc.createEmptyMovieClip("holder",
mc.getNextHighestDepth());
itemsLoader.loadClip(url, holder);
}
}
private function onLoadInit(mc:MovieClip) {
trace("LoadChecker ::: onLoadInit");
itemsLoaded.push(mc);
trace(" - itemsLoaded:"+itemsLoaded.length)
trace(" - itemsToLoad:"+itemsToLoad.length);
if(itemsLoaded.length == itemsToLoad.length){
dispatchEvent({type:"complete"});
}
}
}
Things to do:
- Define an onLoadError function (see MovieClipLoader docs)
- Define an implicit getter/setter for the public itemsToLoad property, so you
can actually do something when it changes.
Or
- Make itemsToLoad private so that it can not be changed from the outside or
define it as a getter (read only).
With the above class, rather than passing a function (to be called when loading finished) to the constructor, you listen for the
complete event
function loaderCompleteHandler(o:Object):Void {
trace("loading finished");
}
var loader:LoadChecker = new LoadChecker(items);
loader.addEventListener("complete" Delegate.create(this, loaderCompleteHandler);
There's more stuff you could do, for instance, not load the items automatically by making the loadItems method public and using a
boolean flag that indicates if items should be automatically loaded or not.
In the init method you'd then check the flag (true/false) and only call
loadItems() when the flag is true:
public var autoLoad:Boolean = true;
private function init() {
// snip
if(autoLoad) loadItems();
}
var loader:LoadChecker = new LoadChecker();
loader.autoLoad = false;
loader.itemsToLoad = items;
// later
loader.loadItems();
This is kinda like the FLVPlayback autoPlay property.
regards,
Muzak
----- Original Message -----
From: "Alistair Colling" <[EMAIL PROTECTED]>
To: "Flash Coders List" <[email protected]>
Sent: Thursday, January 31, 2008 1:25 PM
Subject: Re: [Flashcoders] AS2 Delegate event won't fire in my multi
swfloadclass
Thanks for all of your response, it seems the problem was the onLoadInit event needed to be passed using the Delegate Class,
rather than an inline function:
loadListener.onLoadInit = Delegate.create(this, mcLoaded);
Just in case this class is of any use to anyone here's my finished version.
Many thanks for everyone's suggestions, have a good weekend :)
Ali
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders