So here's a loader that works:
public var loaders:Array = new Array();
public var loaderClip:Array = new Array();
function loadPersonmenu() {
var person:Array = new Array();
var url:Array = new Array();
var cityUrl:Array = new Array();
for (var i = 0; i < personNumber; i++) {
loaders.push(new Loader());
loaders[i].contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
loaders[i].addEventListener(MouseEvent.MOUSE_OVER, lightUp);
person.push("person" + i + ".swf");
url.push(new URLRequest(person[i]));
loaders[i].load(url[i]);
loaders[i].x = ((i + 1) * margin) + (i * videoWidth);
loaders[i].alpha = .5;
container.addChild(loaders[i]);
}
}
Everything is fantastic, super-duper, awesome. But when I add some lines to treat the loader content as a MovieClip (so I can apply the stop()
function to it), the compiler explodes into an error-fiesta. All my pretty external SWFs vanish. The new lines are at the bottom with comments appended:
public var loaders:Array = new Array();
public var loaders:Array = new Array();
function loadPersonmenu() {
var person:Array = new Array();
var url:Array = new Array();
var cityUrl:Array = new Array();
for (var i = 0; i < personNumber; i++) {
loaders.push(new Loader());
loaders[i].contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
loaders[i].addEventListener(MouseEvent.MOUSE_OVER, lightUp);
person.push("person" + i + ".swf");
url.push(new URLRequest(person[i]));
loaders[i].load(url[i]);
loaders[i].x = ((i + 1) * margin) + (i * videoWidth);
loaders[i].alpha = .5;
container.addChild(loaders[i]);
loaderClip[i] = MovieClip(loaders[i].content); // Here is a new line
loaderClip[i].stop(); // Here is another new line
}
}
These are my errors:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at Menu/loadPersonmenu() at Menu/fileCompleteListener() at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at flash.net::URLLoader/onComplete()
The weird thing is, my function fileCompleteListener()
doesn't even reference any of these Loader instances or SWFs, which is why I didn't include it in my code. This isn't a problem in the first conde
You don't seem to be giving the loader chance to load anything, and so loaders[i].content
is null.
Listen for the complete event before trying to access the loader content. Because you are using multiple loaders you should also assign each loader an instance name using i
so that you can use one generic complete listener and reference the loader name to assign the content to the correct index of loaderClip
.
//in the loop
loaders[i].name = i;
loaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
//////////////////////////////////////////////////
//complete handler method
private function loadCompleteHandler(evt:Event):void
{
var index:int = int(Loader(LoaderInfo(evt.target).loader).name);
loaderClip[index] = LoaderInfo(evt.target).content;
}