I have a BuildingSprite that extend Sprite. I try to load external swf library into my main application.
I have this code and it works fine:
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();
.....
}
I created a new class - BuildingSprite that extends Sprite:
package
{
import flash.display.Sprite;
public class BuildingSprite extends Sprite
{
public function BuildingSprite()
{
super();
}
}
}
And from the onAssetsLoaded above, I change the code:
var mySprite:Sprite = new assetClass();
to
var mySprite:BuildingSprite = new assetClass();
I debug and get this error: Main Thread (Suspended: TypeError: Error #1034: Type Coercion failed: cannot convert Tree@c8f0301 to BuildingSprite.)
I think there is a type casting error. Is there any way to load the external swf library and assign it to custom class?