Click to See Complete Forum and Search --> : Load external image with AS3


tarsus
04-07-2009, 12:41 PM
New to ActionScript 3; I tried to search the forum for this solution (which should be simple), but unsuccessfully.

I'm simply trying to load an external image into my movie with AS3. I've boiled my test down to a single-frame movie containing an empty movie clip with instance name "pic." The published SWF is in the same directory as "425_FH000023_cropped.jpg." This code doesn't work:

var picLoader = new Loader();
picLoader.addEventListener(Event.COMPLETE, loadPicture);
picLoader.load(new URLRequest("425_FH000023_cropped.jpg"));

function loadPicture(event:Event):void {
pic.addChild(picLoader);
}

infinityspiral
04-07-2009, 03:32 PM
Here's the tutorial I've used in the past. It's worked for me.
http://www.flash-db.com/Tutorials/loadingAS3/

tarsus
04-07-2009, 04:40 PM
Thanks. Clearly I needed to use URLLoader instead of just Loader. However, it is also apparent that what is passed to addChild must still be a Loader object. So as best I can tell, both still need to be involved. (I'd like to understand a little more about the exact function of both.) Additionally, I don't know how to use event.target to access the image that was loaded into URLLoader, and therefore I'm stuck manually using the path string again. Here's my modified code, which now works at least:

var pathString = "425_FH000023_cropped.jpg";

var picLoader:URLLoader = new URLLoader();
picLoader.addEventListener(Event.COMPLETE, loadPicture);
picLoader.load(new URLRequest(pathString));

function loadPicture(event:Event):void {
var thisLoader:Loader = new Loader();
thisLoader.load(new URLRequest(pathString));
pic.addChild(thisLoader);
}

I'd appreciate it if someone could help me polish this up more. (i.e., solve the issues I mentioned above.)

tarsus
04-07-2009, 05:10 PM
I FINALLY dug up the real answer to this. URLLoader is not required after all; it is simply that Loader has a property called contentLoaderInfo that actually contains the COMPLETE event:

var picLoader:Loader = new Loader();
picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadPicture);
picLoader.load(new URLRequest("425_FH000023_cropped.jpg"));

function loadPicture(event:Event):void {
pic.addChild(event.target.content);
}

NONE of the 40-odd people who viewed this topic could have helped me with that?

infinityspiral
04-10-2009, 11:57 AM
Thanks for posting your solution. :)

Honest Mistake
08-27-2009, 04:45 AM
I have been searching for this solution and you my man have just saved me from going crazy with an overly complicated XML solution. Thanks tarsus!