4 Replies Latest reply on Jul 3, 2007 6:22 AM by gjastrab

    Call Function From a Loaded SWF

    gjastrab Level 1
      I have 2 Flex apps I'm going to run locally on a machine (both compiled using -use-network=false so that I can just run them from the user's machine and access local images, etc.). Let's call them App1 and App2.

      I want to load App2 into App1 and from within App1 call a public function from App2. It appears that this used to be possible in Flex 1.5 ( http://renaun.com/blog/2005/08/30/4/) but I can't compile the following to test.

      I know I can use LocalConnection to talk to two SWFs, but from what I
      gathered from the "Cross Scripting" section of
      ( http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Live Docs_Book_Parts&file=security2_117_07.html)
      it appears you don't need to do so in this case:

      "SWF file loading and cross-scripting are always permitted between SWF files that reside in the same sandbox. For example, any local-with-filesystem SWF file can load and cross-script any other local-with-filesystem SWF file;"

      Here's the code I'm trying to test:

      /* --- App1.mxml --- */

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="600" height="600">

      <mx:Script>
      <![CDATA[
      import flash.events.Event;

      private function completed(evt:Event):void {
      swf.content.sendAlert("sent from parent app");
      }
      ]]>
      </mx:Script>

      <mx:SWFLoader id="swf" source="App2.swf" complete="completed(event);" x="250" y="250" />

      </mx:Application>

      /* === App1.mxml === */

      compiled with: "mxmlc -use-network=false App1.mxml"

      /* --- App2.mxml --- */

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="200"
      height="200">

      <mx:Script>
      <![CDATA
      import mx.controls.Alert;

      public function sendAlert(txt:String):void {
      Alert.show(txt);
      }
      ]]>
      </mx:Script>

      </mx:Application>

      /* === App2.mxml === */

      compiled with: "mxmlc -use-network=false App2.mxml"
        • 1. Re: Call Function From a Loaded SWF
          peterent Level 2
          You almost have it correct. Do this instead:

          public var loadedSM:SystemManager;
          ...
          loadedSM = SystemManager(myLoader.content);
          ...
          App2(loadedSM.application).sendAlert("sent from parent app");

          Remember that even your Application MXML files are classes, so you can cast the loadedSM.application) to your App2 class.
          • 2. Re: Call Function From a Loaded SWF
            gjastrab Level 1
            Now a runtime error:

            Change completed:

            private function completed(evt:Event):void {
            var sys:SystemManager = swf.content as SystemManager;
            trace("aPublicVar: '" + App2(sys.application).aPublicVar + "'");
            }

            The debugger yields:

            "[SWF] C:\projects\loadswf\App2.swf - 278,955 bytes after decompression
            [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
            Fault, completed() at App1.mxml..."
            • 3. Re: Call Function From a Loaded SWF
              gjastrab Level 1
              Peter, it appears that the SWFLoader is emitting the COMPLETE event, but then the Application hasn't finished instantiating itself yet when I'm running my code. I've even broken it down another step to the following:

              private function completed(evt:Event):void {
              var sys:SystemManager = swf.content as SystemManager;
              sys.addEventListener(FlexEvent.APPLICATION_COMPLETE, appLoaded);
              }

              private function appLoaded(evt:FlexEvent):void {
              var app:Application = evt.target as Application;
              var app2:App2 = app as App2;
              trace("applicationComplete dispatched, but app2 is null ?= " + (app2 == null));
              }

              When debugging App1.swf, it loads so that I see the App2 properly loaded into App1, but the trace statement yeilds:

              "[SWF] C:\projects\loadswf\App2.swf - 278,955 bytes after decompression
              [trace] app loaded but null ?= true"

              Any other ideas as to what's going on?
              • 4. Re: Call Function From a Loaded SWF
                gjastrab Level 1
                bump...no one?