我有一個可以擴展Sprite的BuildingSprite。我嘗試將外部swf庫加載到我的主應用程序中。加載外部swf並將其分配給自定義類
我有這樣的代碼,它工作正常:
private function loadBuilding():void{
// this context is necessary to find the shared assets
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
// load in the asset swf
var loader:Loader = new Loader();
var req:URLRequest = new URLRequest("assets/Tree.swf");
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onAssetsLoaded);
loader.load(req, context);
}
private function onAssetsLoaded(_event:Event):void{
// get a reference to the loaded library
var loader:Loader = LoaderInfo(_event.target).loader;
var library:* = loader.content;
var assetClass:Class = loader.contentLoaderInfo.applicationDomain.getDefinition("Tree") as Class;
// create an instance of the shared asset
var mySprite:Sprite = new assetClass();
.....
}
我創建了一個新的類 - BuildingSprite擴展雪碧:
package
{
import flash.display.Sprite;
public class BuildingSprite extends Sprite
{
public function BuildingSprite()
{
super();
}
}
}
而且從上面的onAssetsLoaded,我更改代碼:
var mySprite:Sprite = new assetClass();
到
var mySprite:BuildingSprite = new assetClass();
我調試,並得到這個錯誤: 主線程(暫停:類型錯誤:錯誤#1034:類型強制失敗:無法 轉換樹@ c8f0301到BuildingSprite。)
我覺得這是一個類型轉換錯誤。 有沒有辦法加載外部swf庫並將其分配給自定義類?