Error when file loading

322 views Asked by At

I have a button and progressbar on stage. I tried to load an image in the same folder of the flash but it didn't work. I get the following errors:

1119: Access of possibly undefined property contentLoaderInfo through a reference with static type flash.net:URLLoader.

And:

1067: Implicit coercion of a value of type flash.net:URLLoader to an unrelated type flash.display:DisplayObject.

Here's my code:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;

var myLoader:URLLoader = new URLLoader();

btn_Img.addEventListener(MouseEvent.CLICK, uploadPic);

function uploadPic(event:MouseEvent):void{
   myPb.source = myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
     myLoader.load(new URLRequest("myImage.png"));
    btn_Img = null;
        addChild(myPb);
        removeChild(myPb);
}


function imageLoad(event:Event):void{
        addChild(myLoader);
    btn_Img = null;
}

Could you teach me how to solve this problem? I tried many ways but still didn't get it right.

1

There are 1 answers

0
dhc On

contentLoaderInfo is a property of the Loader class, not URLLoader. Also, you can't pass the Loader itself to addChild-- you need to add the content that was loaded. Finally, addEventListener doesn't return anything, so myPb is a meaningless variable. So, your code should look something like:

var myLoader:Loader = new Loader();

btn_Img.addEventListener(MouseEvent.CLICK, uploadPic);

function uploadPic(event:MouseEvent):void{
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
    myLoader.load(new URLRequest("myImage.png"));
}


function imageLoad(event:Event):void{
    var loaded_image = BitMap(myLoader.content);  // or whatever type the load is
    addChild(loaded_image);
}

Note that you're not checking for errors on the load, which you may want to add in as well.

See adobe help for Loader and LoaderInfo for more details.