pass data to child swf after swf loading completed

元气小坏坏 提交于 2019-12-11 16:48:07

问题


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";
    }
    


来源:https://stackoverflow.com/questions/48354825/pass-data-to-child-swf-after-swf-loading-completed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!