Apache – Flash and Flex

apache-flexflash

I want to embed a Flash movie into Flex (Flex Builder 3), both using Action Script 3, and have my flash movie call functions in Flex and vice versa.

Is this possible?

Best Answer

Yes it is possible. If you want to embed another swf into your flex application you have a few options.

Option 1 - Embed the swf, inline, into a SWFLoader component. This option has some security drawbacks as the swf gets loaded into another application domain and so communication between your flex app and the embedded content can sometimes be difficult. You can give this component an id, listen to the complete event and then talk to the content property of SWFLoader to get access to the loaded swf. Something like:

<mx:Script>
   <![CDATA[
        private function completeHandler(event : FlexEvent) : void
        {
            trace(mySwfLoader.content);
        }
   ]]>
</mx:Script>


<mx:SWFLoader id="mySwfLoader" source="@Embed(source='YourSwf.swf') complete='completeHandler(event)" />

Option 2 - You could use a meta tag to embed the swf as a class and then create an instance of that class in code. This is gives a lot of flexibility but you loose the benefits of being able to add the object declaratively. Something like:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="creationCompleteHandler(event)">
    <mx:Script>
       <![CDATA[

            [Embed(source="mySourceSwf.swf")]
            private var mySourceSwfClass : Class

            private function creationCompleteHandler(event : FlexEvent) : void
            {
                var mySourceSwf  = new mySourceSwfClass();

                myCanvas.addChild(mySourceSwf);

               trace(mySourceSwf);
            }
       ]]>
    </mx:Script>
    <mx:Canvas id="myCanvas" />
</mx:Application>

Option 3 - Don't have a swf at all. Have whatever you want to embed as a UIMovieClip in the fla. You can then create a .swc file whenever you publish that fla. You can then link to that swc file in flex builder. this will then automatically add that MovieClip as referencable in your project (this may need some more investigation on your part). Essentially you can then do something like:

<local:MyMovieClipInSwc id="myMovieClip" />

This way you get the benefits of both worlds, declarative markup and everything within the same application domain. One thing i would say about this method is that Adobe royally f**ked up with the UIMovieClip class and it's performance sucks. Just be aware if you start to use this everywhere you app may become extremely sluggish.

Related Topic