PDA

View Full Version : Library class to stage via Loader



dominus
October 5th, 2007, 06:09 AM
I have one swf(loaded.swf) file that contains a class called "GraphicQ"
I load this file to another swf file(Test.swf) and then try to instantiate the GraphicQ class but in this Test.swf
I manage to get the class definition but when I try to add the child to the stage I get this error:

TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()

And here is the code:



private var ldr:Loader = new Loader();

//constructor
public function Test()
{
var url:String = "loader.swf";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
ldr.contentLoaderInfo.addEventListener(Event.COMPL ETE, onComplete);

}

private function onComplete(event:Event):void {
var tst:MovieClip = event.target.applicationDomain.getDefinition("GraphicQ") as MovieClip;
addChild(tst);
}
}

Charleh
October 5th, 2007, 06:25 AM
Don't you need a new keyword in there somewhere? Not sure how that getDefinition function works but...

var tst:MovieClip = new event.target.applicationDomain.getDefinition("GraphicQ") as MovieClip;

??

dominus
October 5th, 2007, 06:36 AM
no need for the new keyword..

Charleh
October 5th, 2007, 07:03 AM
Well you've got a null object in tst so you need to instantiate it somehow...not really an AS3 guru (having not used it at all)

A quick look at the docs yields:

var runtimeClassRef:Class = applicationDomain.getDefinition("yourClass") as Class;
var someobj:Object = new runtimeClassRef();

So getDefinition just returns a class def, you need to actually instantiate the object (with the new keyword!)

Therefore

var classDef:Class = event.target.applicationDomain.getDefinition("GraphicQ") as Class;
var tst:MovieClip = new classDef();

dominus
October 5th, 2007, 07:19 AM
Absolutely right. It works now. I got the example from somewhere, didn't crossed my mind to look at the docs :h:

Thank you very much.