3 Replies Latest reply on Jul 13, 2009 1:57 PM by Flex harUI

    Getting events from a SWF

    Handycam Level 1

      I have a SWF file created in Flash CS4.  It's made with AS3 and there are buttons and such that dispatch events, such as:

       

      public function clickedMe(event:MouseEvent):void {

           this.dispatchEvent(new Event("clickedTheButton", true));

      }

       

      I have event handlers in the Flash movie to handle events such as these.  My question is, how to do I load the SWF into Flex and listen for these events?  Is it a simple matter of assigning an event listener to the loader that looks for "clickedTheButton"?

        • 1. Re: Getting events from a SWF
          Flex harUI Adobe Employee

          You could listen for the event on the SWF loader since the event is being bubbled (not a recommended practice).  You can also listen in capture phase even if the event is not being bubbled.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

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

          • 2. Re: Getting events from a SWF
            Handycam Level 1

            Why is that not a recommended practice?  I was told before to do it just

            that way... so I am confused.  How would I do it otherwise?

            • 3. Re: Getting events from a SWF
              Flex harUI Adobe Employee

              Bubbling has its purpose.  Input and scrolling and focusevents bubble because the dispatcher of the event can be "anywhere".  Those event names (click, keyDown, scroll) effectively become reserved.

               

              The fact that a particular button got clicked shouldn't require bubbling.  I recommend dispatching that from the "component boundary" (or in your case, the SWF boundary).  That way nobody knows or cares about the internal workings of the SWF so you can change it later if needed, and folks who are interested will listen for it on the SWFLoader.content.  If you bubble, you run the risk that someday you'll add a container that wraps the SWFLoader and forget that you've already used that event name and use it in another child of that container or get a third party component that happens to use that event name and the container that listens for that event from that other child will get the one you bubbled from the SWFLoader and you'll get errors that are really mysterious because there won't be a useful stacktrace.

               

              That said, plenty of folks bubble messages because it is easy and convenient, but I maintain that it is a bad practice.  Bubbling is effectively the same as having a loud conversation on your cellphone in public.  Might save you the time and hassle of finding a private room, but someday something you say will end up being overheard by the wrong person.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

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