pass data to child swf after swf loading completed

2019-08-17 05:04发布

问题:

I am loading one swf file inside a main swf by using swfloader and i want to pass the parameters to the loaded swf. How can i get the loaded child reference to pass data. My sample code is as follows

TestFile1.mxml

public var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, myFun);
loader.load(new URLRequest("/view/flex/TestFile2.swf"), new LoaderContext(false, ApplicationDomain.currentDomain));
viewId.addChild(loader);        

public function myFun(event:Event):void{
    Alert.show("loader.content-"+loader.content); // here alert coming like this [object_TestFile2_mx_managers_SystemManager]
    var testfile2:TestFile2 = loader.content as TestFile2; // here testfile2 is null
    testfile2.param1 = "val1";
}

回答1:

There are 2 options.

  1. If you just need simple startup values, you can pass arguments in the loader string and have TestFile2 grab them on start.

    new URLRequest("/view/flex/TestFile2.swf?param1=val1")

  2. If you need to interact with the child, you need to grab a reference to it after the application complete event. Event.COMPLETE only fires when the loader is loaded. In the Event.COMPLETE event, add an event to fire when the content is ready.

    public function myFun(event:Event):void{
        Alert.show("loader.content-"+loader.content); // here alert coming like this [object_TestFile2_mx_managers_SystemManager]
        loader.content.addEventListener(FlexEvent.APPLICATION_COMPLETE, appCreationComplete);
    }
    private function appCreationComplete(event:FlexEvent):void
    {
        var testfile2:TestFile2 = loader.content["application"] as  TestFile2; // here testfile2 is not null
        testfile2.param1 = "val1";
    }