Skip navigation
Currently Being Moderated

Tutorial: Accessing function from external swf with SWFLoader

Feb 15, 2014 3:35 PM

Tags: #swfloader #localconnection #flex4.6

Hello Ladies & Sirs,

 

i am working with Spark ( Flex SDK 4.11, SWFLoader and LocalConnection ).

 

Method 1: SWFLoader and LocalConnection:

The local connection only works the function because the function is the string. The application loads the file of the address. The external file has published its own function, which you can access from the SWFLoader or engage in parallel.

 

This is Main.swf:

<?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"
                                 creationComplete="initApp(event)">
          <fx:Script>
                    <![CDATA[
                              import mx.events.DragEvent;
                              import mx.events.FlexEvent;

                              protected var con:LocalConnection;

                              protected function initApp(event:FlexEvent):void
                              {
                                        btnCall.addEventListener(MouseEvent.CLICK, btnClick);
                                        con = new LocalConnection();
                                        con.addEventListener(StatusEvent.STATUS, onConnect);
                                        swfLoader.addEventListener(DragEvent.DRAG_COMPLETE, onCompleted);
                              }

                              private function btnClick(event:MouseEvent):void
                              {
                                        con.send("appdomain", "openPanel");
                              }

                              protected function ClosePanel(event:MouseEvent):void
                              {
                                        con.send("appdomain", "closePanel");
                              }

                              private function onCompleted(event:Event):void
                              {
                                        swfLoader.autoLoad = true;
                              }

                              private function onConnect(event:StatusEvent):void
                              {
                                        switch (event.level){
                                                  case "status":
                                                            labelStatus.text = "Panel loaded from external swf ;-) and it works fine :)";
                                                            break;
                                                  case "error":
                                                            labelStatus.text = "Error: It can't call..."
                                                            break;
                                        }
                              }
                    ]]>
          </fx:Script>
          <fx:Declarations>
                    <!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
          </fx:Declarations>
          <s:Button left="10" bottom="10" label="Open Panel" id="btnCall"/>
          <s:Label id="labelStatus" left="205" right="10" bottom="10" height="21" paddingLeft="2"
                               paddingRight="2" paddingTop="2" textAlign="left" verticalAlign="middle"/>
          <s:SWFLoader id="swfLoader" left="10" right="10" top="10" bottom="39" scaleContent="true"
                                         source="MyPage.swf"/>
          <s:Button left="108" bottom="10" label="Close Panel" click="ClosePanel(event)"/>
</s:Application>

 

And this is MyPage.swf:

<?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"
                                 creationComplete="initApp(event)">
          <fx:Script>
                    <![CDATA[
                              import mx.events.FlexEvent;

                              import spark.components.Alert;

                              protected var con:LocalConnection;

                              protected function initApp(event:FlexEvent):void
                              {
                                        con = new LocalConnection();
                                        con.client = this;
                                        try{
                                                  con.connect("appdomain");
                                        }catch(error:ArgumentError){
                                                  Alert.show("Error: It can not connect your correct domain.");
                                        }
                              }

                              public function openPanel():void
                              {
                                        this.panel.visible = true;
                              }

                              public function closePanel():void
                              {
                                        this.panel.visible = false;
                              }

                    ]]>
          </fx:Script>
          <fx:Declarations>
                    <!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
          </fx:Declarations>
          <s:Panel id="panel" visible="false" width="250" height="200" horizontalCenter="0"
                               title="Open from external SWF!" verticalCenter="0">
          </s:Panel>

</s:Application>

 

If you run application than it works fine.

And there are examples of pictures:

http://my.funpic.de/show-photo/803489-PIC.png

http://my.funpic.de/show-photo/803490-PIC.png

Yeah it works good.

 

Method 2: SWFLoader and SystemManager:

The system manager can access through external files via variables and functions. System Manager can perform everything when you click the main file or side file, the SWF files can affect all functional.

 

I am working with FlexLib 2.5 ( MDICanvas and MDIWindow) Now i have used Main.swf which loads MyPage.swf. This works fine.

 

This is Main.swf:

<?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"
               xmlns:flexlib="http://code.google.com/p/flexlib/"
               creationComplete="creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.managers.PopUpManager;
            import mx.managers.SystemManager;

            import spark.components.Alert;

            [Bindable]
            private var extSM:SystemManager;

            private var _externalSwfLoaded:Boolean;

            [Bindable]
            public function get externalSwfLoaded():Boolean
            {
                return _externalSwfLoaded;
            }

            public function set externalSwfLoaded(value:Boolean):void
            {
                _externalSwfLoaded = value;
            }

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                var context:LoaderContext = new LoaderContext();
                context.applicationDomain = ApplicationDomain.currentDomain;
                loader.loaderContext = context;
                loader.load("MyPage.swf");
            }

            protected function loaderCompleteHandler(event:Event):void
            {
                extSM = loader.content as SystemManager;
                //    Set the swf loaded flag if you have the SystemManager
                if (extSM)
                    externalSwfLoaded = true;

            }

            protected function loaderIoErrorHandler(event:IOErrorEvent):void
            {
                trace(event.toString());
                Alert.show(event.type + ":" + event.text, "IO error occured");

            }

            protected function openPropreties(event:MouseEvent):void
            {
                Object(extSM.application).openPropretiesWin();
                canvas.windowManager.add(Object(extSM.application).openPropretiesWindow);
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
    </fx:Declarations>
    <s:SWFLoader id="loader" left="0" top="0" bottom="40" width="100%"
                 complete="loaderCompleteHandler(event)" ioError="loaderIoErrorHandler(event)"
                 trustContent="true"/>
    <s:Button left="150" bottom="10" label="Open Propreties" click="openPropreties(event)"/>
    <flexlib:MDICanvas id="canvas" left="0" right="0" top="0" bottom="39">
    </flexlib:MDICanvas>
</s:Application>

 

And this is MyPage.swf:

<?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"
               backgroundAlpha="0" creationComplete="initApp(event)">
    <fx:Script>
        <![CDATA[
            import flexlib.mdi.containers.MDIWindow;

            import mx.events.FlexEvent;

            [Bindable]
            public var openPropretiesWindow:MDIWindow;

            public function openPropretiesWin():void
            {
                openPropretiesWindow = new MDIWindow;
                openPropretiesWindow.width = 300;
                openPropretiesWindow.height = 400;
                openPropretiesWindow.title = "Propreties";
            }

            protected function initApp(event:FlexEvent):void
            {
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
    </fx:Declarations>
</s:Application>

 

If I run the application, it works fine.

 

There are examples of pictures:

http://i.stack.imgur.com/f43Ge.png

http://i.stack.imgur.com/T8sVF.png

http://i.stack.imgur.com/CFkjT.png

 

They are original swf files:

http://i.stack.imgur.com/HBQVK.png


Thank you for support and help I have improved post because i have bad english. I am sorry for that. Thank you!
I would like to apologize for you

 

// EDIT: I am sorry for that because spark components won't work from loading swf application when you open MDIWindow with same components like TextInput or TextArea and any components and they can not work after openning MDI-WIndow when Flash player was debugged since events stop again

 

It is very bad

 

I found better solution: We use only mx-component ( Flex SDK 3.6.0a or 3.7. They component ( <mx...> very better than spark components...

 

This is MainDesktop.swf

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                                        xmlns:flexlib="http://code.google.com/p/flexlib/"
                                        creationComplete="creationCompleteHandler(event)" layout="absolute">
          <mx:Script>
                    <![CDATA[
                              import mx.controls.Alert;
                              import mx.events.FlexEvent;
                              import mx.managers.SystemManager;
  
                              [Bindable]
                              private var extSM:SystemManager;
  
                              private var _externalSwfLoaded:Boolean;
  
                              [Bindable]
                              public function get externalSwfLoaded():Boolean
                              {
                                        return _externalSwfLoaded;
                              }
  
                              public function set externalSwfLoaded(value:Boolean):void
                              {
                                        _externalSwfLoaded = value;
                              }
  
                              protected function creationCompleteHandler(event:FlexEvent):void
                              {
                                        var context:LoaderContext = new LoaderContext();
                                        context.applicationDomain = ApplicationDomain.currentDomain;
                                        loader.loaderContext = context;
                                        loader.load("PropretiesApp.swf");
                              }
  
                              protected function loaderCompleteHandler(event:Event):void
                              {
                                        extSM = loader.content as SystemManager;;
                                        //    Set the swf loaded flag if you have the SystemManager
                                        if (extSM)
                                                  externalSwfLoaded = true;
  
                              }
  
                              protected function loaderIoErrorHandler(event:IOErrorEvent):void
                              {
                                        trace(event.toString());
                                        Alert.show(event.type + ":" + event.text, "IO error occured");
  
                              }
  
                              protected function openPropreties(event:MouseEvent):void
                              {
                                        Object(extSM.application).openPropretiesWinCall();
                                        canvas.windowManager.add(Object(extSM.application).propretiesWindow);
  
                              }
  
                    ]]>
          </mx:Script>
          <flexlib:MDICanvas left="0" right="0" top="0" bottom="40" id="canvas">
                    <mx:SWFLoader left="0" right="0" top="0" bottom="40"
                                                    id="loader" trustContent="true"
                                                    complete="loaderCompleteHandler(event)"
                                                    ioError="loaderIoErrorHandler(event)"/>
          </flexlib:MDICanvas>
          <mx:Button left="10" bottom="10" label="Open Propreties" click="openPropreties(event)"/>
  
</mx:Application>

 

This is PropretiesApp.swf:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                                        layout="absolute"
                                        backgroundAlpha="0">
          <mx:Script>
                    <![CDATA[
                              import flexlib.mdi.containers.MDIWindow;
  
                              import mx.controls.TextArea;
  
                              import net.sourceskyboxer.PropretiesWindow;
  
                              [Bindable]
                              public var propretiesWindow:PropretiesWindow;
  
                              public function openPropretiesWinCall():void
                              {
                                        propretiesWindow = new PropretiesWindow();
                                        propretiesWindow.width = 600;
                                        propretiesWindow.height = 400;
                              }
                    ]]>
          </mx:Script>
  
</mx:Application>

 

and this is component "PropretiesWindow":

<?xml version="1.0" encoding="utf-8"?>
<ns:MDIWindow xmlns:mx="http://www.adobe.com/2006/mxml"
                                xmlns:ns="http://code.google.com/p/flexlib/"
                                horizontalScrollPolicy="off" layout="absolute" title="Propreties"
                                verticalScrollPolicy="off">
          <mx:RichTextEditor left="-9" right="-9" top="-28" bottom="-9" title="Titel">
          </mx:RichTextEditor>
</ns:MDIWindow>

 

I am sorry for worng name like "Propreties WIndow"  or "Editor Window".

I create component from propretiesWindow because i am testing for different version for spark and mx. Because mx components are very better than spark components.

Look at picture

http://my.funpic.de/show-photo/805652-PIC.png

This works awesome!!!!!!!! Why does spark framework not work for loading swf application. That is crazy because spark components have not single components because they are using rsl format

 

MX-Components have not problems for compressed single code. This works only MDIWindow with one or more components... I am very happy because we have used old version

 

Best use only mx components  regards and greeting from Germany,

Jens Eckervogt.

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points