5 Replies Latest reply on May 25, 2010 2:15 AM by Karl_Sigiscar_1971

    flash call the method of flex application

    QiuDingliang

      In a flex application I use the SWFLoader to load a flash,the code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12">
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;

       

      public function testResult():void{
              Alert.show("Hello World");
      }
      ]]>
      </mx:Script>
      <mx:SWFLoader id="flash" source="flash/test3.swf"/>
      </mx:Application>

       

      There's a button in the embeded flash(flash/test3.swf),

      when I click this button,how to call the the testResult method of the flex application?Thank you!

        • 1. Re: flash call the method of flex application
          samschauhan Level 1

          In your Action Script of (flash/test3.swf) do an:

           

          import mx.core.Application;

           

          and then in your button:

           

          <mx:Button label="Button" click="Application.application.testResult();"/>

           

           

          Regards,

          SAM

          • 2. Re: flash call the method of flex application
            Karl_Sigiscar_1971 Level 3

            You can dispatch an event from Flash and have testResult be the listener for that event in Flex.

            • 3. Re: flash call the method of flex application
              QiuDingliang Level 1

              The (flash/test3.swf)  is not a flex Application,it's a normal flash file!

              • 4. Re: flash call the method of flex application
                BhaskerChari Level 4

                Hi,

                 

                You can make use of complete event of SwfLoader and within that function get the instance of Flash Buttonm and register an evnet for the click within the Flex app as shown below:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12">
                    <mx:Script>
                    <![CDATA[
                    import mx.controls.Alert;
                   
                    private var loadedSWFMainTimeline:*;
                   
                    public function testResult(event:MouseEvent):void{
                         Alert.show("Hello World");
                    }
                   
                    public function onSWFLoadComplete():void {
                         //loadedSWFMainTimeline gets you a reference to the Flash SWF MainTimeline
                         loadedSWFMainTimeline = flash.content;
                         if(loadedSWFMainTimeline)
                         {   
                            var _button:SimpleButton = loadedSWFMainTimeline.flash_button_instance as SimpleButton;
                            _button.addEventListener(MouseEvent.CLICK,testResult);       
                           
                         }
                    }
                   
                    ]]>
                    </mx:Script>
                <mx:SWFLoader id="flash" source="flash/test3.swf" complete="onSWFLoadComplete();"/>
                </mx:Application>

                 

                 

                Note: In the above code "flash_button_instance" is the instance name of the Flash button.

                 

                If this post answers your question or helps, please kindly mark it as such.

                 

                 

                Thanks,

                Bhasker Chari

                • 5. Re: flash call the method of flex application
                  Karl_Sigiscar_1971 Level 3

                  >> The (flash/test3.swf)  is not a flex Application,it's a normal flash file!

                   

                  Precisely. And that's why using a mx package in it is forbidden. A swf created with Flash Professional can dispatch an event that can be caught by a Flex app that loads it. That's the best way to call your Flex method from Flash.