14 Replies Latest reply on Jun 18, 2009 8:37 AM by Bram Van Oost

    Control external SWF's

    Bram Van Oost

      Hi all,

       

      I need to create a Flex application that contains various Flash animations (SWF), and I must be able to control them.

      To start with: how do I control embedded SWF's?

       

      This is what I have:

       

      <mx:SWFLoader id="swfObj" source="@Embed('swf/testfla.swf')"  />

       

              private function init():void{
                  swfObj.object.addEventListener("thisButton",handler);
              }

       

           (thisButton is an instance in the SWF)

       

              private function handler(ev:Event):void{
                  swfObj.content.startClip();

       

              }

       

           (startClip(); is a function in the swf that starts the movie)

       

       

      Doesn't do much! :-)

      Help is of course very appreciated.

        • 1. Re: Control external SWF's
          Flex harUI Adobe Employee

          "thisButton" is a button or an event from the SWF?

           

          If it is a button, and it fires a click event then you need to do:

           

          swfObj.content["thisButton"].addEventListener("click", handler);

           

          Note that the Flash SWFs must be published for FP9 and AS3, otherwise you need to use a different technique.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

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

          • 2. Re: Control external SWF's
            Bram Van Oost Level 1

            Hi Alex,

             

            Thanks a lot for your response.

            "thisButton" is the instance name of a movie clip.

             

            The code still gives the error:

            ReferenceError: Error #1069: Property thisButton not found

             

            Am I missing something here?

            • 3. Re: Control external SWF's
              dzeikei Level 2

              If you want to use functions on your SWFs other than the usual MovieClip functions like play(), stop() you will have to do something different.

              The way I do it is, create an interface for all the functions and properties you would want to access in the SWF file then cast swfObj.content into that Interface. After that you can call all the functions and properties no problemo. Of course, the SWF will have to implement the interface or the casting will fail...

              Actually, since MovieClips are dynamic Classes, you can cast swfObj.content into MovieClip then use all the functions. Using Interface helps to prevent runtime errors though

              • 4. Re: Control external SWF's
                Bram Van Oost Level 1

                dzeikei,

                 

                Could you give a more concrete example?

                This is all fairly new to me (but I tend to pick up quite swiftly).

                 

                Thanks!

                • 5. Re: Control external SWF's
                  dzeikei Level 2
                  <mx:SWFLoader id="swfObj" source="@Embed('swf/testfla.swf')"  />
                  <mx:Script>
                  <![CDATA[
                  
                       private var _swfObject:MovieClip;
                  
                       private function init():void{
                            _swfObject = swfObj.content as MovieClip;
                            _swfObject.thisButton.addEventListener(MouseEvent.CLICK,handler);
                       }
                       private function handler(ev:MouseEvent):void{
                            _swfObject.startClip();
                       }
                  

                  This is the quick and dirty method that should work from my memory!

                  • 6. Re: Control external SWF's
                    Bram Van Oost Level 1

                    No, still get the same error that thisButton can not be found.

                    (doublechecked the SWF, it's def the instance name of a movie on the scene)

                    • 7. Re: Control external SWF's
                      Bram Van Oost Level 1

                      Still no ball, didn't realise this wasn't an obvious thing to do between Flex and Flash SWF's.

                       

                      In short:

                      - I need to access the variables and functions within an embedded SWF

                      - starting and stopping the SWF would also be nice, but mc.stop(); or mc.start(); do nothing (mc = swfObj.content)

                       

                      I thank thee all yet another time!

                      • 8. Re: Control external SWF's
                        dzeikei Level 2

                        It should be working, maybe it's the Flash file is in AS2 as Flex harUI mentioned.

                        AS3 SWF and AS2 SWF cannot interact in this way, you will have to use LocalConnection

                        • 9. Re: Control external SWF's
                          Bram Van Oost Level 1

                          Hi dzeikei, it's definitely AS3.

                           

                          <mx:SWFLoader id="swfObj" source="@Embed('swf/testmotion.swf')" creationComplete="creationComplete();"/>

                           

                          <mx:Script>
                               <![CDATA[

                                    import flash.display.MovieClip;

                                    public var mc:MovieClip = new MovieClip;

                                    public function creationComplete():void {

                                         mc = swfObj.content as MovieClip;
                                         mc.stop();

                                    }

                               ]]>
                          </mx:Script>

                           

                          This should be pretty straightforward, right?

                          Come to think of it.. am I controlling the source of swfObj when i try to manipulate mc?

                          • 10. Re: Control external SWF's
                            dzeikei Level 2

                            I didn't believe you so I tried it myself... and you are right!

                            My method works for dynamically loaded SWFs but embedded SWFs seem to behave differently!

                            Embedded SWFs are nested within yet ANOTHER loader which is not a property of the MovieClip so you will have to fetch the child directly. Good thing there is only 1 child. Try this:

                             

                            <mx:SWFLoader id="swfObj" source="@Embed('swf/testmotion.swf')" creationComplete="creationComplete();"/>
                            <mx:Script>
                                <![CDATA[
                                      import flash.display.MovieClip;
                                      public var mc:MovieClip = new MovieClip;
                                      public function creationComplete():void {
                                          mc = ( ( swfObj.content as MovieClip ).getChildAt(0) as Loader ).content as MovieClip;
                                          mc.stop();
                                      }
                                ]]>
                            </mx:Script>
                            
                            • 11. Re: Control external SWF's
                              Bram Van Oost Level 1

                              Hey Dzeikei,

                               

                              That totally did it! Thanks so much! :-)

                               

                              Which leads me to the two following questions (I thought they would have figured themselves out by now):

                              - how do I access functions in the SWF?

                              - how can I read out variables from that SWF?

                               

                              James, if you're ever in Belgium, beers. Lots of them, on me. ;-)

                              • 12. Re: Control external SWF's
                                dzeikei Level 2

                                Just use functions and properties as you normally would. You won't get 

                                code hinting or compile time error checking so make sure everything is 

                                correct to avoid runtime error.

                                Mmmm beer...!

                                • 13. Re: Control external SWF's
                                  Bram Van Oost Level 1

                                  Ok, functions seem to work great. Oof.

                                  Now to get variables from the SWF and update them inside the SWF.

                                   

                                  Thanks a lot, man! Community support ftw.

                                  • 14. Re: Control external SWF's
                                    Bram Van Oost Level 1

                                    Got it!

                                     

                                    mc["testVar"];

                                    Or straight to an instance with mc.object.instancename.text.

                                     

                                    Thanks again! Beers! :-)