Hi everyone,
I’m making a Flash game and have a Parent MovieClip into which Child swfs (levels) are loaded. The Parent has an inventory system that will store the objects found in the Children. Of course, to do that, I need access to the each Child’s base class and all its assets, so I use GetDefinition.
Everything was fine before I bumped into a weird problem: it calls the Child’s constructor twice. Before I go further, here’s the code stripped down to its simplest form.
PARENT
package{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
public class Root extends MovieClip
{
private var ldr:Loader;
private var k1:MovieClip;
public function Root()
{
ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.INIT, onLoaded, false, 0, true);
ldr.load(new URLRequest("Kid1.swf"));
}
private function onLoaded(e:Event):void
{
var Kid1Class:Class = e.target.applicationDomain.getDefinition("Kid1");
k1 = new Kid1Class();
addChild(k1);
}
}
KID
package
{
import flash.display.MovieClip;
public class Kid1 extends MovieClip
{
public function Kid1():void
{
trace("THIS IS KID1");
}
}
}
I really can’t figure out why the message “THIS IS KID” is being outputted twice. If I omit “k1 = new Kid1Class();”, it shows only once. So does it mean that calling GetDefinition will inherently call the target object’s constructor as well?
The only workaround I’ve found for the time being is to have an init() function in the Child that the Parent calls after loading. Nevertheless, this mystery is nagging me. Can anyone shed light on this?
Much obliged.