1 Reply Latest reply on Mar 7, 2008 2:11 PM by ntsiii

    Communicating with a loaded swf

    Masterchief10
      Hey Guys,

      We need to be able to communicate between two swfs (both written in flex 2.0.1), one loaded into the other with a SWFLoader. Have read about using the content property of the swf loader, but we get the following errors:

      1 Error #1034: Type Coercion failed: cannot convert mx.containers::Canvas@162660a1 to flash.display.MovieClip.

      2 Error #1069: Property ShowTotal not found on _Expenses_mx_managers_SystemManager and there is no default value.

      Loader:

      private var swfPage:SWFLoader = new SWFLoader();

      swfPage.id = "expenses";

      swfPage.name = "expenses";

      swfPage.load("Expenses.swf");

      swfPage.addEventListener("ready", thingyrun);

      expenseHolder.addChild(swfPage);


      private function thingyrun(e:Event):void

      {

      var mc:MovieClip = swfPage.content as MovieClip;
      try
      {
      Alert.show(mc.ShowTotal());
      }
      catch (er:Error)
      {
      Alert.show("1 " + er.message);
      }
      try
      {
      Alert.show(mc.ShowTotal());
      }

      catch (er:Error)
      {

      Alert.show("2 " + er.message);

      }

      }



      Loadee (called when a button in this swf is pressed, so has definitely loaded)



      public function ShowTotal(obj:Object):String

      {
      return "Total is " + a_total.text;
      }


      If anybody has had similar issues and figured out how to solve it, we would be greatly appreciative if you could share your knowledge.
        • 1. Re: Communicating with a loaded swf
          ntsiii Level 3
          Yep, the two most difficult things about working with SWFLoader are data types and timing.

          First, a reference your loader will need to be in instance scope. This will not do what you expect:
          swfPage.id = "expenses";
          It will not make the loader available using the "expenses" id as a reference. I see you are not trying to do that and are using the "swfPage" variable, which presumably you have declared in instance scope.

          Next, there are two steps to the timing issue. Even though the loader is "ready", the app that is being loaded is probably still instantiating. You need to listen for the application's complete event, before interacting with the content app.

          Here is a complete but simple example that shows all the parts.
          http://www.cflex.net/showfiledetails.cfm?ChannelID=1&Object=File&objectID=690

          Tracy