7 Replies Latest reply on May 17, 2007 8:39 AM by Mitek17

    Communicate Flash with Flex

    Santanu Karar Level 1

      I wonder if we can communicate Flash with Flex..! Is it possible..! I heard somewhere, yes, it is possible.. though doesn't have any resource to see how this thing works..!

      Can anybody share any article/example link on this..! This will help me a lot..!


        • 1. Re: Communicate Flash with Flex
          peterent Level 2
          How Flex and Flash communicate depends on what versions you are talking about. Specifically which versions of ActionScript. Flex is ActionScript 3. Flash 8 and older are ActionScript 2. Flash 9 CS3 is ActionScript 3.

          For Flex and ActionScript 2 Flash SWFs you must use LocalConnection. See my blog (weblogs.macromedia.com/pent and search for LocalConnection to find the article).

          For Flex and ActionScript 3 Flash you have a whole new world. I've also written an article on my blog about that.
          • 2. Re: Communicate Flash with Flex
            Mitek17 Level 1
            Hi aSuitableBoy,

            You won't find much info about Flex-Flash communication. All the info I've seen on the net was just an excited articles in blogs where the simplest case of accessing SWF from Flex application is described.

            I've managed to communicate Flex & Flash and the apps are sharing the code. Which means that Flex app has data and Flash app has visual objects and methods to control them.

            The run-time communication is simple - you load SWF in Flex and when content of SWF is ready just access the data structures inside of the movie.
            If you don't have any shared data, just a methods - it's very straight and simple.

            The more tricky part is to share the data structures across the apps. This a moment where you need a shaman's dance around it.

            If you have any technical questions - please ask.

            • 3. Re: Communicate Flash with Flex
              Santanu Karar Level 1
              Hi Mitek,

              Thanks for your reply. And thanks also to Peterent for his nice help. You guys are really helpful!

              Yes, its true that the forums all have described only how to access SWF from Flex application. But if I need to call Flex methods inside from that Flash or need to pass events, might share data also .. then how should be the approach. Though I still have not very clear idea if LocalConnection can also be able a SWF to send command to Flex or not..!

              Mitek, can you share us some example that you've done communicating Flex and Flash both with each other..?

              It'll be very exciting to see the things are in action.


              • 4. Re: Communicate Flash with Flex
                Mitek17 Level 1
                Hi ASB,

                First of all - instead of LocalConnection use another way.
                1. In Flex: Declare SWFLoader, set source property to your SWF and place a listener for "init" event. When "init" event comes it means that SWF contents are accessible:
                var l_mcSWFContent:MovieClip = event.target.content;

                and you can do something like this:


                This part is the simplest and it should work without a problem

                2. Next step - Flash.
                In Flash I would split the drawings as objects and the methods to control them.
                The drawing should be exported as a symbol in library and symbol linkage settings should be:
                - give it a class name - which is this case instance name
                - set Export for Actionscript
                - set export in the first frame

                Second part - the actionscript which controls the drawing's behavior.
                I place it in the standalone .as file

                package dir_name {

                //CSharedData is declared in Flex
                import myflexproject.common.data.CSharedData;

                public class CMyObject

                private var m_Data:CSharedData;


                Then you need to initialise your Flash drawing in Flash - do it in the first frame.
                Something like
                var m_instance: CMyObject = new CMyObject();

                (And later in Flex you can access it using mcSWFContent.m_instance.some_method())

                This is the basic theory. After you do this shaman's dance begins.

                Initially I worked with Flash 9 Alpha - and I solved all the problems integrating Flex & Flash.
                A couple of days ago I tried Flash CS3 and everything stopped working.
                The problems I've encountered so far:

                - Flash CS3 does not understand package names. It understands it but in very strange manner, for example if .as file is in the same directory as .fla file package name should be empty. Weird is too simple word to describe this.

                - It does not understand the classpath in the .fla file settings. All classpaths should be declared globally in Flash CS

                - Flash AS is not compatible with Flex AS. I don't understand this advertising jabber about full AS3 compatibility, but if you compare Adobe\Flex Builder 2\Flex SDK 2\frameworks\source\mx\ and Adobe\Adobe Flash CS3\en\First Run\Classes\mx\"
                you will see that in Flex has 10 times more classes then Flash. Which means that you will not be able to use all of them in Flash.

                What I did - I added Flex mx directory to the global Flash classpath. I don't know why it works - probably at the runtime Flash SWF has an access to the Flex symbol library.

                Everything I mentioned above was OK to Flash 9 Alpha. My application worked fine, except some run-time errors which are fixed in Flash CS3.

                If you are lucky everything will be fine and your Flex-Flash link will be OK.

                Unfortunately, I've got a problem with a couple of SWF out of ~10. Flash CS does not want to compile some of my Flex code - it complains on mx_internal namespace (error 1004).
                I am currently trying to find a workaround for this, as this is a complete show-stopper. Probably I will need to make copy of the Flex framework and patch it, so Flash will be able compile the code.

                Hope this helps.
                Please don't hesitate to contact me - I know that there is no much info about this area I spent quite a lot of time getting this things work.

                It's a shame that Adobe's documentation is currently below any standard and you have to google like a gold-digger through hundreds of useless blogs to find anything relevant to your problem..


                • 5. Re: Communicate Flash with Flex
                  peterent Level 2
                  Flash CS3 is not supposed to have Flex classes; that will come later as the products evolve. You won't be able to compile Flex code in Flash CS3 right now. However, the AS3 compiler in Flash CS3 is the same one that's in Flex 2.

                  You can use packages in Flash CS3 and have them work in Flex 2. You need to export a SWC from Flash CS3 to do that. If you use a SWF it won't work.

                  The vision is not to bring in SWFs created with Flash CS3 but to bring in classes and instantiate them as any other Flex component. To do that you need to use a SWC.

                  For example, suppose you want to make a Clock in Flash. You'd like to set the time programmatically and have the clock dispatch an event on the half-hour. You create a Flash symbol for the clock and any additional assets/symbols you need. You create an AS3 class file for it, perhaps my.components.Clock stored in the file my/components/Clock.as. The Clock.as file as Event meta data for the half-hour chime and a public setter function for the time. It also has overrides for updateDisplayList to do anything programmatically, like move the hands.

                  In Flash CS3 you select the Clock movie clip symbol and pick "Make Flex Component" (using the Flex Kit for Flash CS3 available on Adobe Labs). Now you publish a SWC from your Flash file.

                  In Flex Builder, you add that SWC to your compiler path. Then you can instantiate the clock:

                  import my.components.Clock;
                  var c:Clock = new Clock();
                  c.time = new Date();

                  If you are not using Flash CS3, then you must use LocalConnection since the AS3 SWFs and the AS3 SWFs live in different VMs within the Flash Player and you do not have access to their classes and data members.
                  • 6. Re: Communicate Flash with Flex
                    peterent Level 2
                    One more thing: if you are having trouble with the documentation, PLEASE file a bug report or enhancement request. We can't fix if we don't know it is broken.


                    • 7. Re: Communicate Flash with Flex
                      Mitek17 Level 1
                      Hi Peter,


                      Originally posted by: peterent
                      You won't be able to compile Flex code in Flash CS3 right now.

                      So far it is possible. My understanding is because both SWFs live in the same VM, Flex classes are available to both of them. At least my code works without visible side effects.


                      The vision is not to bring in SWFs created with Flash CS3 but to bring in classes and instantiate them as any other Flex component. To do that you need to use a SWC

                      This will be great. We just started a year ago and there was no AS3 in Flash at that time. And when Flash 9 Aplha arrived I just figured out the way how to do this using what I've got. and of course - there was no information as to where it would go - just rumors in the blogs.


                      One more thing: if you are having trouble with the documentation, PLEASE file a bug report or enhancement request. We can't fix if we don't know it is broken.

                      Peter, documentation is not broken - it's just missing. It covers well top-level things but if you need to go on the low-level - there is just nothing. To understand how it works, I've examined the generated code, still it does not show the core level.

                      And also it's a pity that Adobe supports a blogging system instead of users/support forums on Adobe's website. I've spent a couple of days figuring out why SWF does not get a focus in Mozilla. And I found an explanation in archives of some flash users forum - somebody mentioned that it was a bug in Player and Adobe knows about it. Well,why is it not traceable on Adobe's forums/live docs/known problem lists etc?

                      Another point - I've submitted a couple of bugs to Support - and they are invisible to me or to others. I have no idea if it has been already reported by others or they are new ones.

                      My gut feeling is that there should be a single information exchange centre, rather then 5-10 blogs running by Adobe professionals and hundreds of blogs which people just post how they feel about Flex and sometimes the references to yours or Aral's articles. It's a good marketing step, but there is also should be information for professionals who need something more rather than "Hello world" in AS.

                      If there were such a place, I would be happy to contribute.