我正在学习 ActionScript 3.0,我经常看到这种范例:
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);
}
}
}
我想做的是取回对象,而不是将加载器视为DisplayObject。我将发布我的理想化版本,希望它能最好地解释我想做的事情。
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;
}
}
}
我缺少的是为什么 while 循环永远不会退出。无论如何,这是我想要完成的事情,所以请随时修改我的示例或发布您喜欢的方法。谢谢!