From AS2 to AS3 loading external images

actionscript-2actionscript-3flashimagemigration

I'm converting some Actionscript code from AS2 tp AS3, and I've eventually managed to get most of it to work again (it's allmost a totally different language, sharing just a little syntax similarity). One of the last things that still doesn't work, is the code for loading an external image.

Perhaps this has changed in AS3 but I really thought it was strange that to load an image you use loadVideo, why not loadImage? (on the other hand a flash application is constantly called a flash video even when it's not used for animation at all). This doesn't work anymore, and what I've found is a pretty complex code that is said to replace this oneliner imageholder.loadVideo(url); is this:

var urlreq:URLRequest = new URLRequest(url);
var theloader:Loader = new URLLoader();
theloader.load(urlreq);
theloader.addEventListener(Event.COMPLETE, function(event:Event):void {
        imageholder.addChild(theloader);
    }
);

But this doesn't work.. What I am doing wrong, and is there a more suited function to load images in AS3?

Best Answer

One of most common problems people have with loaders is what they listen to for events. You need to make sure you're listening to the loader.contentLoaderInfo for most events, not the loader itself. That was the main problem in the example you provided.

//this is WRONG
loader.addEventListener(Event.COMPLETE, onLoad);

//this is RIGHT
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
Related Topic