Hello.
I am learning ActionScript 3.0 and I am seeing this paradigm often:
package util
{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
public class BitmapLoader extends Sprite
{
private var loader:Loader = new Loader();
private var filePath:String;
public function BitmapLoader(filePath)
{
this.filePath = filePath;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
}
public function load():void
{
loader.load(new URLRequest(filePath));
}
private function completeListener(event:Event):void
{
addChild(loader);
}
}
}
What I would like to be able to do is get the object back rather that treat the loader as a DisplayObject. I will post my idealized version in the hopes that it will best explain what I would like to do.
package util
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
public class BitmapLoader
{
private var hasLoaded:Boolean = false;
private var loader:Loader = new Loader();
private var filePath:String;
private var bitmap:Bitmap;
public function BitmapLoader(filePath)
{
this.filePath = filePath;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
}
public function load():Bitmap
{
loader.load(new URLRequest(filePath));
while(!hasLoaded);
return bitmap;
}
private function completeListener(event:Event):void
{
bitmap = new Bitmap(Bitmap(LoaderInfo(event.target).content).bitmapData);
hasLoaded = true;
}
}
}
What I'm missing is why the while loop never exits. Anyhow, this is sort of what I'd like to accomplish, so feel free to amend my example or post you preferred methods. Thanks!