【发布时间】:2011-06-12 22:17:15
【问题描述】:
我有一个扩展 Sprite 的 BuildingSprite。我尝试将外部 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();
.....
}
我创建了一个新类 - 扩展 Sprite 的 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();
我调试并收到此错误: 主线程(暂停:TypeError:错误#1034:类型强制失败:不能 将 Tree@c8f0301 转换为 BuildingSprite。)
我认为存在类型转换错误。 有没有办法加载外部 swf 库并将其分配给自定义类?
【问题讨论】:
标签: flash actionscript-3