# How to reference Loader object through listener?

**URL:** <https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052>\
**Category:** flash\
**Created:** [May 22, 2008, 5:16pm UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052 "2008-05-22T17:16:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [May 22, 2008, 5:16pm UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/1 "2008-05-22T17:16:20Z")

</div>

How do I reference Loader object through a listener?  
for example:

```auto

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;
}

```

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 22, 2008, 5:40pm UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/2 "2008-05-22T17:40:45Z")

</div>

I think this is how you would go about it

```auto

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.

---

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [May 23, 2008, 12:01am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/3 "2008-05-23T00:01:49Z")

</div>

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

```auto
[/as] :) [/ot]
```

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [May 23, 2008, 1:05am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/4 "2008-05-23T01:05:33Z")

</div>

e.target.loader

---

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [May 23, 2008, 1:08am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/5 "2008-05-23T01:08:26Z")

</div>

EDIT: thanks Krilnon! 😃

---

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [May 23, 2008, 1:49am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/6 "2008-05-23T01:49:45Z")

</div>

when i use e.target.loader on a listener listening for PROGRESS or OPEN, i get this error:

```auto

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:

```auto

function onLoadProgress(e:Event):void{
    var index:int = _imgLoaders.indexOf(e.target.loader);
    var holder:Sprite = new Sprite();			
   _imgHolders[index] = holder;
}

```

---

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [May 23, 2008, 7:20am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/7 "2008-05-23T07:20:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Alex\_Lexcuk](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Alex\_Lexcuk](https://forum.kirupa.com/u/Alex_Lexcuk)\
**Post date:** [May 23, 2008, 10:09am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/8 "2008-05-23T10:09:18Z")

</div>

[http://dnadillo.dn.ua/mur\_gallery/php/AC3\_PHP.swf](http://dnadillo.dn.ua/mur_gallery/php/AC3_PHP.swf)  
[http://dnadillo.dn.ua/mur\_gallery/php/GaleryScoresText.txt](http://dnadillo.dn.ua/mur_gallery/php/GaleryScoresText.txt)  
[http://dnadillo.dn.ua/mur\_gallery/php/mur\_galery\_AC3\_PHP.rar](http://dnadillo.dn.ua/mur_gallery/php/mur_galery_AC3_PHP.rar)

---

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [May 24, 2008, 4:57am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/9 "2008-05-24T04:57:49Z")

</div>

^ 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?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [May 24, 2008, 6:33am UTC](https://forum.kirupa.com/t/how-to-reference-loader-object-through-listener/261052/10 "2008-05-24T06:33:56Z")

</div>

It’s probably the length of _loaderArray_, so yes.
