I usually add this to projects:
RemoteImage.as
Actionscript Code:
package com.egoant
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class RemoteImage extends Sprite
{
private var triggerFunction:Function;
public function RemoteImage(imageURL:String, triggerFunction:Function = null):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, InitializeImage);
loader.load(new URLRequest(imageURL));
this.triggerFunction = triggerFunction;
}
private function InitializeImage(e:Event):void
{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var displayObj:DisplayObject = loaderInfo.loader;
displayObj.x = 0 - displayObj.width / 2;
displayObj.y = 0 - displayObj.height / 2;
addChild(loaderInfo.loader);
if (triggerFunction != null)
{
triggerFunction.call(null, this);
}
var midSprite:Sprite = new Sprite();
midSprite.x = 0;
midSprite.y = 0;
midSprite.graphics.lineStyle(1);
midSprite.graphics.drawCircle(0, 0, 4);
addChild(midSprite);
}
}
}
(I call this one RemoteImage because it will load from a remote server as well)
Then if I have a class that needs an image I do something like this:
Actionscript Code:
var cardImage:RemoteImage = new RemoteImage("myImageURL.png", null);
cardImage.x = 10;
cardImage.y = 10;
addChild(cardImage);
Then all you have to do is pass the URL of the card image to this when you initialize it. I can keep the image names associated with a card in a database or XML file that way, and load them from the server as needed.
One drawback to this method is that in some cases the same image might load multiple times, which you can work around if you are careful.