How do I reference Loader object through a listener?
for example:
function Main():void{
var loader:Loader = new Loader();
loader.load(new URLRequest("image.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadCompleted);
}
function onLoadCompleted(e:Event):void{
//I have no access to loader here.
//How would i access it? So i could reposition, rotate, etc.
//ie. loader.x = 50;
}
I think this is how you would go about it
public class FooBar
{
private var loader:Loader;
public function Main():void{
loader = new Loader();
loader.load(new URLRequest("image.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadCompleted);
}
private function onLoadCompleted(e:Event):void{
loader.x = 50;
}
}
I think that it is unable to access it in onLoadCompleted() because loader is local to Main(). If you make loader an object property, it will be accessible to all of the methods of the class.
But I’m no AS expert, so I could easily be wrong.
BTW, how do you turn on the syntax highlighting for AS code? I only see the option for PHP.
i know all about the scope thing, but i really need to reference it through the listener.
you see, i have an array of Loaders, and i want to do something to those loaders individually when they finish loading.
[ot]the tags for AS are
[ /as] :) [/ot]
when i use e.target.loader on a listener listening for PROGRESS or OPEN, i get this error:
Error: Error #2099: The loading object is not sufficiently loaded to provide this information.
at flash.display::LoaderInfo/get loader()
at Main/imageStart()[F:\My Documents\John PC\Lactopafi\Carousel\src\Main.as:86]
it works fine on the COMPLETE event
my code looks something like this:
function onLoadProgress(e:Event):void{
var index:int = _imgLoaders.indexOf(e.target.loader);
var holder:Sprite = new Sprite();
_imgHolders[index] = holder;
}
thanks amarghosh. I’ll try that… but one question, why are you casting loaderArray to Loader? I’m assuming loaderArray is an array of Loaders, shouldn’t loaderArray* already be a Loader object?
^ i’m sorry, but what does that have to do with this topic?
@amarghosh: what does the totalFiles variable refer to? the number of Loader objects?
It’s probably the length of loaderArray, so yes.