Getting a reference to a class in a swf

Well I’ve been searching the web and knocking my head against the wall long enough to justify an inquiry.

Anybody know an elegant way to get a usable reference to a class in a loaded swf?

I find that this approach, from Big Spaceship, produces an error, when compiler is forced to try to convert type “Ball” to type “MovieClip.” Sheesh.


var loader:Loader = new Loader();
var nuSquare:MovieClip;
var nuBall:MovieClip;
var req:URLRequest = new URLRequest("Callclass.swf");
loader.contentLoaderInfo.addEventListener(Event.INIT, sq);
loader.load(req);
 
 
function getAsset($str:String):MovieClip {
    var c:Class = Class(getDefinitionByName($str));
    return new c();
}
 
function sq(evt:Event):void {
    var nuball:MovieClip = MovieClip(getAsset("Ball"));
    addChild(nuball);
}


But meantime this approach, which really makes very little sense to me, succeeds.

I’m thinkin’ there’s gotta be a better way. Any tips much appreciated.


function sq(e:Event):void {
    var squ:Class = e.target.applicationDomain.getDefinition("Square") as Class;
    nuSquare = new Square();
    addChild(nuSquare);
    var c:Class = Class(getDefinitionByName("Ball"));
    var b:Ball = new Ball();
    addChild(b);
}