Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Flex/Air Accessing movieclip in loaded swf

In one of my latest’s works, I had to use two different swfs and Flex to interact between them. For example, I had some movie clips in one swf, and in other I had some visual text that I wanted to show on click in one of the movie clips. Cause the information that I found wasn’t so god, here is a example how can you do it.

What do you really have to do is initialize the movie clips and functions inside of each swf, and then you can use them as you which.

First:

Create 2 swf (I used Flash).  In one we have a movie clip that I called BtnClick and in the second one I have a label with visibility set to false, and a function in that allow me to show the label.

function FunctionInsideSWF(event:MouseEvent):void
{
        if(TxtHello.visible)
        {
                TxtHello.visible = false;
        }
        else
        TxtHello.visible = true;
       
}

Second: In Flex, use swfLoader to load your content and then initialize the movie clips and the functions that you made is Flash :

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                           xmlns:s="library://ns.adobe.com/flex/spark"
                           xmlns:mx="library://ns.adobe.com/flex/mx"
                           minWidth="400" minHeight="100"
                           >
        <fx:Script>
                <![CDATA[
                       
                        import mx.controls.Button;
                        private var loaderContent1:Object;
                        private var loaderContent2:Object;
                       
                        protected function InitMovieClips(event:Event):void
                        {
                                var BtnClick:SimpleButton = SimpleButton(loader1.content["BtnClick"]);
                                BtnClick.addEventListener(MouseEvent.CLICK, ShowHelloWorldText);
                                loaderContent2 = loader2.content;
                        }
                       
                        protected function ShowHelloWorldText(event:MouseEvent):void
                        {
                                loaderContent2.FunctionInsideSWF(event);
                        }
                       

                ]]>
        </fx:Script>
       
        <fx:Declarations>
                <!– Place non-visual elements (e.g., services, value objects) here –>
        </fx:Declarations>
       
        <mx:HBox backgroundColor="#CCCCCC">
                <mx:SWFLoader id="loader1" source="assets/Loader1.swf" complete="InitMovieClips(event)"/>
                <mx:SWFLoader id="loader2" source="assets/Loader2.swf" complete="InitMovieClips(event)"/>      
        </mx:HBox>
       
</s:Application>

Link to the project

And that’s it…

Hope you enjoy

Posted on by Fábio Belga This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply