Search code examples
actionscript-3actionscriptflash-cs4

Actionscript: loader not found


I am trying to load an image onto my stage. I use the following code:

    public function loadImg():void{

        var iLoader:Loader = new Loader();
        iLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
        iLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);

        var fileRequest:URLRequest = new URLRequest(imgPath+'testimg.JPG');
        iLoader.load(fileRequest);

    }

    public function onProgressStatus(e:ProgressEvent) {   
        trace(e.bytesLoaded, e.bytesTotal); 
    }

    public function onLoaderReady(e:Event) {     
        this.stage.addChild(iLoader); // error is here
    }

However it seems that iLoader is not found in onLoaderReady:

1120: Access of undefined property iLoader.

How do I pass object iLoader to this function? Or am I doing something wrong?

Thanks for your help in advance! :D


Solution

  • See this page on Function scope. Variables defined inside a function are only available within it, so you just need to define them outside of the function:

    private var iLoader:Loader;
    
    public function loadImg():void{
    
        iLoader = new Loader();
        //...
    
    }