AS2 to AS3 upload external images

I am converting some ActionScript from AS2 tp AS3 and I ended up managing to get most of them working again (it's almost a completely different language, sharing only slight syntax similarities). One of the last things still not working is the code to load the external image.

This may have changed in AS3, but I really thought it was weird that to load the image you are using loadVideo

, why not loadImage? (on the other hand, a flash app is consistently called flash video, even if it's not used for animation at all). This does not work anymore and what I found is quite 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 it doesn't work. What am I doing wrong and is there a better function for loading images in AS3?

+1


source to share


3 answers


I think you are making things too complicated - this is what you need to upload a single image:

import flash.display.Loader;
import flash.event.Event;
import flash.net.URLRequest;

var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event){
 // e.target.content is the newly-loaded image, feel free to addChild it where ever it needs to go...
});
imageLoader.load(new URLRequest('yourImage.jpg'));

      



... can you extrapolate from there to load a list of images? Run this code in a for-loop, assigning a different variable (an array location, perhaps, or an object property) each time to your newly loaded e.target.content so that you can access it outside of that type of onComplete function.

+1


source


One of the most common problems people run into with downloaders is that they are listening for events. You need to make sure that you are listening to loader.contentLoaderInfo for most events and not the loader itself. This was the main problem in the example you gave.



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

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

      

+3


source


I opened my soul myself.

Initializing or unloading a list of images (actually MovieClip instances):

for (i = 0; i<imgHolders.length; i++) {
    var loader:Loader = imgHolders[i].getChildByName("imgloader"+i);
    if (loader) {
        loader.unload();
    } else {
        loader = new Loader();
        loader.name = "imgloader" + i;
        // Does't seem to work, commented out.
        //loader.addEventListener(Event.COMPLETE, centerimage);
        imgHolders[i].addChild(loader);
    }
}

      

when i need to change the image:

var loader:Loader = imgHolders[index].getChildByName("imgloader"+index);
loader.load(new URLRequest(newurl);

      

I tried adding a listner too to center the image, but it doesn't seem to be called? The first thing the centerimage function does is that it trace("centerImage");

doesn't appear at all ...

0


source







All Articles