How to run an external SWF inside a Flex Application?

后端 未结 5 1771
慢半拍i
慢半拍i 2021-02-03 15:18

EDIT: Due to the answer I change the code posted. I\'ve added the Security.allowDomain(\"*\") line and that line throws me an error. So, ho

5条回答
  •  天涯浪人
    2021-02-03 15:31

    You can try to load your SWF temporarily into a ByteArray and then load it with your SWFLoader.

    Don't forget to set allowCodeImport to true since your SWF has as code inside.

    Of course be sure that your loaded swf is secure enough for your application since it will have access at all your property.

    private function loadSwfApplication():void {
      // load the file with URLLoader into a bytearray
      var loader:URLLoader=new URLLoader();
    
      // binary format since it a SWF
      loader.dataFormat=URLLoaderDataFormat.BINARY;
      loader.addEventListener(Event.COMPLETE, onSWFLoaded);
    
      //load the file
      loader.load(new URLRequest("path/to/the/application.swf"));
    }
    private function onSWFLoaded(e:Event):void {
     // remove the event
     var loader:URLLoader=URLLoader(e.target);
     loader.removeEventListener(Event.COMPLETE, onSWFLoaded);
    
     // add an Application context and allow bytecode execution 
     var context:LoaderContext=new LoaderContext();
     context.allowCodeImport=true;
    
     // set the new context on SWFLoader
     sfwLoader.loaderContext = context;
    
     sfwLoader.addEventListener(Event.COMPLETE, loadComplete);
    
     // load the data from the bytearray
     sfwLoader.load(loader.data);
    }
    
    // your load complete function
    private function loadComplete(completeEvent:Event):void {
     var swfApplication:* = completeEvent.target.content;
     swfApplication.init();  // this is a Function that I made it in the Root 
                             // class of swfApplication
    }
    

提交回复
热议问题