6 Replies Latest reply on Jun 8, 2009 2:11 PM by Jason Lampitt

    How to capture Flash SWC events?

    Jason Lampitt Level 1

      Can someone point me in the right direction on how to capture an event from a Flash SWC in Flex?

       

      Scenario:

       

      I have an animated gameboard that I built in Flash. Nothing fancy, just a simple tile game where you select a tile, click it and it flips over to reveal something.

       

      I've managed to export a SWC, and get it to display just dandy in my Flex project, but I cannot figure out how to get Flex to capture and respond to any type of event... custom or predefined (such as MouseEvent.CLICK) when clicking one of the tiles in the swc.

       

      I've done quite a bit of googling but I'm cross-eyed at this point and could use some expert direction.

       

      Thanks in advance.

       

      JL

        • 1. Re: How to capture Flash SWC events?
          Flex harUI Adobe Employee

          It depends on what objects dispatch the event from the Flash classes and how you've integrated them into the Flex app.  You won't be able to use MXML, but you should be able to call addEventListener on the right objects.  Some events can also be listened for in capture phase.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

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

          • 2. Re: How to capture Flash SWC events?
            Jason Lampitt Level 1

            Hi Alex,

             

            After further research, I've found some answers to my questions, but I've encountered a related obsticle.

             

            I finally stumbled upon the Flex 3 Component Kit documentation, and on page 9 of that document, it describes in detail an example of adding custom events.

             

            To summarize, my related obsticle this:

             

            • I created a simple MovieClip symbol in Flash (named "My Circle").
            • I created a simple external class mimicing the example code in the Flex 3 Component Kit documentation (named "MyCircle.as") which extends mx.flash.UIMovieClip
            • The .fla containing the MovieClip symbol "My Circle" and the external class "MyCircle.as" reside in the same directory
            • In Flash I convert the symbol "My Circle" to a Flex component and verify that the class is "MyCircle" and there is no base class
            • I export the now converted MovieClip as a .swc file
            • In my Flex project, I add MyCircle.swc to the Library path
            • In my Flex project source I add an instance of MyCircle as <local:MyCircle id="my_circle" />
            • Switching to Design view in my Flex project I don't see anything. I refresh design view and still do not see a graphic representation of my .swc file. There are no errors or warnings
            • Using the Outline, I select the instance of MyCircle and discover that it is indeed on the stage, but it's bounding box dimensions are essentially zero. It seems there is no image in the swc.

             

             

            Returning to Flash, I duplicated the MovieClip symbol, renamed it to "My Circle No External Class", and converted it to a Flex component. I also verified that this new component had a class name of "MyCircleNoExternalClass" and had a base class of mx.flash.UIMovieClip.

             

            Following the above proceedure to correctly link it to my Flex project's Library path and adding an instance of <local:MyCircleNoExternalClass /> to the source, the .swc file show's up beautifully.

             

            So, why would adding an external class file (with the ultimate goal of being able to dispatch custom events) cause the contents of the MovieClip itself to not be included in the .swc?

             

            Again, I'm following the example on page 9 of the Flex 3 Component Kit documentation and using the Flex 3.3 SDK.

            • 3. Re: How to capture Flash SWC events?
              Flex harUI Adobe Employee

              Don't know for sure.  It sounds like asking the FLA to generate a class based on UIMovieClip does something different than assigning your own class.  We'd have to dig through all the bits to find out.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

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

              • 4. Re: How to capture Flash SWC events?
                Jason Lampitt Level 1

                Well...

                 

                If you can point me in the right direction, I'll do the grunt work.

                 

                Can you give me a few more details on, "assigning your own class"?  I'm not sure what you mean. According to the documentation, you have to create a custom class that extends UIMovieClip for the entire SWC. Are you suggesting to create a specific class for, say, a specific button inside the SWC itself that would generate the event on something like a MouseEvent.CLICK?

                • 5. Re: How to capture Flash SWC events?
                  Flex harUI Adobe Employee

                  By "assigning your own class" I meant that, if I understood you correctly, when you assigned an external class file to the symbol, it didn't work.

                   

                  Disclaimer: this is not my area of expertise so I might be missing some important fact, but here's what I how I see it.

                   

                  From a SWF perspective, in AS3 everything is a class and gets linked in.  Flex has rules though and one of the pertinent rules is that Flex Container children must be IUIComponents.  Another pertinent rule is that UIComponent children can be anything.

                   

                  For DesignView to show your component in a Flex Container, it must be an IUIComponent and that's why we put together the Flex Component Kit.  If you just wanted to get some Flash class to show up in Flex from ActionScript you can always write code that simply instantiates the class and adds it as a child to a UIComponent (but not a container).

                   

                  In a similar vein, for you to put a custom component in MXML in a Container, it must also be an IUIComponent, and in order to use MXML syntax like 'click="do some code"', you need Event metadata on the class.

                   

                  But from Actionscript you can just say "new MyClass()" and myClassInstance.addEventListener("click", ...).

                   

                  So, it depends on whether you want to use MXML and design view.  If you don't need it, you can use a Flash SWC pretty much as is.  If you do, then the question is what went wrong on your external class attempt.  You could rename the SWC to .zip, extract its SWF and use SWFDump to see what is different.

                   

                  HTH

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

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

                  • 6. Re: How to capture Flash SWC events?
                    Jason Lampitt Level 1

                    I haven't had the chance to follow up on this, but I will post continued findings within the next week or so.