ActionScript does not seem to support loading a png synchronously. I have tried loading it asynchronously, but then there is the problem of making your function wait for the load to finish. I cannot accomplish this. I have tried calling both setInterval and setTimout in a while loop, and breaking when the load is complete, but I get some very mysterious behavior. For the function parameter, I pass a function called doNothing that is just an empty function. When I run, setInterval or setTimeout gets over and over, very quickly. They do not wait the time provided before calling again. Also, doNothing never gets called.
Here's my code:
private function getData(extension:String):Bitmap
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onComplete);
loader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onComplete);
loader.load(new URLRequest(baseDir + extension));
while (bitmap == null)
{
var test:Number = setInterval(doNothing, 1000);
}
var ret:Bitmap = bitmap;
bitmap = null;
return ret;
}
function onComplete(event:Event):void
{
bitmap = Bitmap(LoaderInfo(event.target).content);
}
function doNothing():void
{
trace("did nothing");
}