1

I would like to load external child swf into the parent swf (under same directory). I did try, the child loaded but the class in it didn't work.

1) How can i load the class of child swf?

2) How can i unload the class of child swf?(becoz there could be many ext. swf with different class)

thx

main.fla

 function startLoad(){
    var mLoader:Loader = new Loader();
    var mRequest:URLRequest = new URLRequest("game1.swf");
    var mLoaderContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
    mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load(mRequest, mLoaderContext);
}

function onCompleteHandler(loadEvent:Event){
    var keyManager:Class = ApplicationDomain.currentDomain.getDefinition("net.keithhair.KeyManager") as Class;
    addChild(loadEvent.currentTarget.content);
}

game1.fla

    import net.keithhair.KeyManager;

    keyManager=new KeyManager(stage);
    keyManager.addKey(["a"], doSomething);
    function doSomething():void {
    //do something  
    }

Result:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at net.keithhair::KeyManager/removeListeners()
at net.keithhair::KeyManager/addListeners()
at net.keithhair::KeyManager()
at game1_fla::MainTimeline/frame1()
frankyG
  • 11
  • 4
  • Have you tried this? http://stackoverflow.com/questions/1634757/as3-instantiate-class-from-external-swf – Eric Norcross Aug 01 '13 at 00:41
  • tried. same result: TypeError: Error #1009: Cannot access a property or method of a null object reference. at net.keithhair::KeyManager/removeListeners() at net.keithhair::KeyManager/addListeners() at net.keithhair::KeyManager() at main_fla::MainTimeline/fooLoadComplete() – frankyG Aug 01 '13 at 08:10

1 Answers1

0
function Constructor(){
   if (stage){
      onAddedToStage();
   } else {
      addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
   }
}

function onAddedToStage(evt:Event=null):void {
    var keyManager:KeyManager;
    trace("here!")
    keyManager=new KeyManager(stage);
    keyManager.addKey(["a"], doSomething
}
Constructor();

The above code solve all my problem. thank you very much destinier & senocular

frankyG
  • 11
  • 4