7 Replies Latest reply on Aug 4, 2009 8:33 AM by danwize

    Loading and Manipulating external swfs

    danwize

      I'm new to the Flex development environment.  In the past, when we develped a large application in Flash, we would load swfs and pass relevant data to them.  I've created a flex application and would like to load its compiled swf from another flex project.  I can do this and the swf plays, but how can I get access to the loaded swf's properties and methods?

       

      I have been loading flash assets by converting them to flex custom components using Flex Component Kit, but I don't want to have to put all of my flash content in components and add them to one large project becuase this would make for a huge swf if all those swcs are included at compile time.  Is there a way to load a swf and access its attributes?  Or do I have to rework the way I build a large application that consisted of many swf files in the past?

        • 1. Re: Loading and Manipulating external swfs
          Gregory Lafrance Level 6

          This is a great LiveDocs page, and will answer your question:

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=controls_15.html

           

          If this post answered your question or helped, please mark it as such.

          1 person found this helpful
          • 2. Re: Loading and Manipulating external swfs
            danwize Level 1

            That page was very helpful!  Thanks.  I'm still missing one piece though.  They showed how to acces properties of a swf, but not methods.

             

             

            Example:
            loadedSM.application["lblOne"].text = "I was just updated.";
            
            But, how can I call a function in a loaded swf and pass arguments to it?
            
            example:
            loadedSM.application["myFunc"];  //can I call a function this way?  
            Where do I put the args? Any help would be awesome!
            • 3. Re: Loading and Manipulating external swfs
              danwize Level 1

              After looking a little closer I see the answer to my question.
               
              FlexApp(loadedSM.application).setVarOne("Updated varOne!");
               
              What is this FlexApp though?  Is that just the name of the swf file without the .swf?  I tried using that method, but my swf is not in the same project folder. It looks like they're casting loadedSM.application as a FlexApp, where is FlexApp defined unless it is just the swf name.
               
              example:
               
              <mx:SWFLoader id="myLoader" width="300"
                       source="C:/Users/danwize/documents/youGetTheIdea/FlexApp.swf"
                       creationComplete="initNestedAppProps();"/>
               
              when I do this, can I still put FlexApp(loadedSM.application).setVarOne("Updated varOne!");     ?

              • 4. Re: Loading and Manipulating external swfs
                Gregory Lafrance Level 6

                FlexApp is the name of the SWF without the .swf extension. As long as you have resolved to the location of the SWF file, probably in the "source" property of the SWFLoader, you should be able to use the name of your SWF file.

                 

                If this post answers your question or helps, please mark it as such.

                • 5. Re: Loading and Manipulating external swfs
                  danwize Level 1

                  I'm still unable to get this to work.
                   
                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                  <mx:Script>
                    <![CDATA[
                         import mx.managers.SystemManager;
                     
                         [Bindable]
                         public var loadedSM:SystemManager;
                     
                         private function passObjects():void
                         {
                      loadedSM = SystemManager(myLoader.content);
                      //SoccerGame(loadedSM.application).receiveObjects();  //THIS IS THE LINE WHERE I GET THE ERROR.  Call to possibly undefined method SoccerGame
                         }
                    ]]>
                  </mx:Script>
                    <mx:SWFLoader source="SoccerGame.swf" creationComplete="passObjects();" id="myLoader"/>
                  </mx:Application>

                  • 6. Re: Loading and Manipulating external swfs
                    Flex harUI Adobe Employee

                    SWFs are loaded asynchronously, so they aren't guaranteed to be there by creationComplete.  The protocol is to use the Event.COMPLETE to listen for FlexEvent.APPLICATION_COMPLETE from the loadedSM, then loadedSM.application should be valid.

                     

                    Note that if you type-cast the application, you end up linking the application's classes into the main SWF, defeating the point of having a sub-application.  Use an interface, or just use loadedSM.application["receiveObjects"]()

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

                    Blog: http://blogs.adobe.com/aharui

                    • 7. Re: Loading and Manipulating external swfs
                      danwize Level 1

                      Thank you!  That is exactly what I'm looking for!  I definately didn't want to link the sub application into the main mxml. For anyone else out there who's looking to do this, here is some sample code.

                       

                      <?xml version="1.0" encoding="utf-8"?>

                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

                      <mx:Script>

                      <![CDATA[

                      import mx.events.FlexEvent;

                      import mx.managers.SystemManager;

                       

                      [Bindable]

                      public var loadedSM:SystemManager;

                       

                       

                      private function passObjects():void

                      {

                      loadedSM = SystemManager(myLoader.content);

                      loadedSM.addEventListener(FlexEvent.APPLICATION_COMPLETE, callFunc);

                      }

                       

                       

                      private function callFunc(evt:FlexEvent):void

                      {

                      loadedSM.application["receiveObjects"]();

                      }

                      ]]>

                      </mx:Script>

                      <mx:SWFLoader source="SoccerGame.swf" id="myLoader" complete="passObjects();"/>

                      </mx:Application>