So I have been trying to do something like the following in AS3 to load any image and retrieve its bitmap data during runtime:
var bitmapData:BitmapData;
var loader:Loader;
var completed:Boolean = false;
public function loadBitData(path:String):BitmapData{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest(path));
while(!completed){
}
completed = false;
return bitmapData;
}
private function onComplete (event:Event):void{
bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
completed = true;
}
Since the loader will not load the file on a separate thread, this code will obviously get stuck in an infinite loop. If you remove the while loop the method will return null because by the time it reaches that statement the data has not been loaded. It will still load the data afterwards however. (I have checked this using a progress event and printing the amount of data that has been loaded so far)
I just want the method to load the file and then return the BitmapData once it has been fully loaded. The program should wait and not execute any more code until this happens. Any help on this would be greatly appreciated.