I'm trying to load a bunch (about 100) of images into memory, so I can display them at various times. Originally I had simply embedded them, but that of course resulted in the swf file being larger than I wanted. So now I'm trying to change it to load them in the background because they aren't needed immediately.
There are probably plenty of problems with this. The current on is that I'm getting an error that says 'TypeError: Error #1034: Type Coercion failed: cannot convert "foo.jpg" to Class.'
I've been googling this awhile, assuming loading an external image is a common thing. That's where I got the Loader and URLRequest code, but I'm clearly missing something. Maybe it's due to my goofy looping logic.
Here's the class so far
public class CardImages2
{
public static var fooImage1:Class;
public static var fooImage2:Class;
public static var fooImage3:Class;
public static var fooImage4:Class;
private static var curImgClass:Class;
public static function load():void {
// map of cards and their urls
var dict:Dictionary = new Dictionary;
dict[fooImage1] = "fooImage1.jpg";
dict[fooImage2] = "fooImage2.jpg";
dict[fooImage3] = "fooImage3.jpg";
dict[fooImage4] = "fooImage4.jpg";
var url:String;
var loader:Loader = new Loader();
var urlReq:URLRequest;
for each(var key:Class in dict) {
url = String(dict[key]);
curImgClass = key;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadListener);
urlReq = new URLRequest(encodeURI(url));
loader.load(urlReq);
}
}
private static function loadListener(e:Event):void {
curImgClass.source = Class(e.currentTarget.content);
}
}