9 Replies Latest reply on Aug 30, 2010 10:53 PM by shashank Kulkarni

    Click event on SWFLoader

    carlos_sousa4

      Hello,

       

      In my FLEX application i have a SWFLoader where i load some wsf files that are located in others places in the web. Some of the swf files loaded contain an hyperlink associated and, when the swf file is clicked, a new web page is opened.

      So, i need to count the number of clicks made in the SWFLoader but, in the case when the swf file has an hyperlink associated, the click event is not fired. What can i do to detect if a click was made in the SWFLoader?

       

      Thanks in advance for your help.

        • 1. Re: Click event on SWFLoader
          BhaskerChari Level 4

          Hi carlos_sousa4,

           

          Check the below code loading the Flash SWF in to Flex application.. and handling a click event on the Flash SWF Button in the Flex app..

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
          layout="vertical"
          backgroundAlpha="0"
          backgroundColor="#FFFFFF">
          <mx:Script>
            <![CDATA[
             import mx.controls.Button;
             import mx.controls.Alert;
             private var loadedSWFMainTimeline:*;
            
             public function onSWFLoadComplete():void {
              Alert.show("onSWFLoadComplete");
              //loadedSWFMainTimeline gets you a reference to the Flash SWF MainTimeline
              loadedSWFMainTimeline = swfLoader.content;
              if(loadedSWFMainTimeline)
              {
               var _button:SimpleButton = loadedSWFMainTimeline.button_btn as SimpleButton;
               _button.addEventListener(MouseEvent.CLICK,onMCButtonClick);
              }
             }
            
             public function onMCButtonClick(event:MouseEvent):void {
              if(loadedSWFMainTimeline)
              {
               Alert.show("HELLO SWF LINK CLICKED");
              }   
             }
            ]]>
          </mx:Script>
          <mx:SWFLoader id="swfLoader" source="assets/flashswf.swf" complete="onSWFLoadComplete();"/>
          </mx:Application>

           

           

          Note : In the above code  button_btn is the Flash button instance of the SWF file and the corresponding type in the Flex will be SimpleButton...In your post you said that you are using a HyperLink so may be you should flash.text.TextField datatype instead of SimpleButton.

           

          Try to debug by putting a break point at onSWFLoadComplete function and watch the loadedSWFMainTimeline varibale you will come to know the exact type and the other parameters in the SWF file.

           

           

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


          Thanks,

          Bhasker Chari

          • 2. Re: Click event on SWFLoader
            carlos_sousa4 Level 1

            Hi Bhasker,

             

            Thanks for your suggestions.

             

            I've tried it and it seems to be in the correct way but i have a problem in the following line:

             

                 loadedSWFMainTimeline = swfLoader.content;

             

             

            The error message is the following:

            SecurityDomain 'http://localhost/valsousa/flex/bin/FrontOffice.html?debug=true' tried to access incompatible context 'http://www.valedosousa.tv    /pub/investir_pacosdeferreira.swf'
            SecurityError: Error #2121: Security sandbox violation: Loader.content: http://localhost/valsousa/flex/bin/FrontOffice.swf cannot access http://www.valedosousa.tv/pub/investir_pacosdeferreira.swf. This may be worked around by calling Security.allowDomain.

             

             

            The swf file that i include in my application is reference by a url and it seems that i can't access the file to get its content. I have tried the work around suggested without succes  with this two lines:

             

            Security.allowDomain(swfLoader.source);

              Security.allowDomain(swfLoader.content);

             

             

            How can i solve this new problem?

             

            Thanks.

            • 3. Re: Click event on SWFLoader
              BhaskerChari Level 4

              Hi carlos_sousa4,

               

              Try setting Security.allowDomain("http://www.valedosousa.tv/");

               

              Also set trustContent="true" on <mx:SwfLoader /> tag

               

              <mx:SwfLoader trustContent="true" />

               

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


              Thanks,

              Bhasker Chari

              • 4. Re: Click event on SWFLoader
                carlos_sousa4 Level 1

                Hi,

                 

                I'm sorry for taking too much time for responding.

                 

                I've done a work around for the security question but my problem is still not solved because i get an error in this line suggested by Bhasker:

                 

                var _button:SimpleButton= loadedSWFMainTimeline.button_btn as SimpleButton;

                 

                 

                The swf that i include in my flex application is somewhere in the web and i haven't access to the source code of the swf file. I just include it in the application, therefore i don't know what kind of component exist in the swf file. In the line above, i get an error because the cast is not valid.

                 

                 

                I've done more searchs about this problem and it seems to be very complex.

                The swf file that i include in my application has an infinite z-index value and it will catch all the click events. So, i can't handle those events in my application.

                 

                What suggestions can you give to me?

                 

                Thanks in advance.

                 

                Message was edited by: carlos_sousa4

                • 5. Re: Click event on SWFLoader
                  shashank Kulkarni

                  Hi Bhaskar,

                   

                  I do have the same problem, Where in my SWF files are dynamically loaded documents. So how to identify words with the link from same object.

                   

                  Can we capture the event in such scenario?

                   

                  Thanks

                  • 6. Re: Click event on SWFLoader
                    BhaskerChari Level 4

                    Hi SRKKing,

                     

                    You can access the flash Text field placed in Flash SWF file in Flex in the same way as I did for Button where I got a reference to the Flash Button which

                     

                    is a SimpleButton type in Flex...and registered a eventListener in Flex...

                     

                    Same way you can get a reference to the TextField placed in flash in flex and register an eventListener in Flex....

                     

                    If you can send me the sample SWF file you are working I can help you better...

                     

                    Thanks,

                    Bhasker Chari

                    • 7. Re: Click event on SWFLoader
                      shashank Kulkarni Level 1

                      HI Bhaskar,

                       

                      Thanks for Reply,

                       

                      At my application swf loaded are not customized at our end.

                       

                      Let me Elaborate,

                       

                      We have one component to View SWF. The SWF are the word document converted into SWF with the help of Java API.

                      here in this case  i want to capture the link click inside these SWFs from Flex based swf Viewer.

                       

                      I tried what is been suggested by you,

                       

                             public function onSWFLoadComplete(ev:Event):void {
                                  Alert.show("onSWFLoadComplete");
                                  //loadedSWFMainTimeline gets you a reference to the Flash SWF MainTimeline
                                  loadedSWFMainTimeline = loader.content as Object;

                      // This is the Button i am getting from SWF
                                  var  simpleButton:SimpleButton =   loadedSWFMainTimeline.button1162 as SimpleButton;

                               

                      //But None of the following works correctly

                                 simpleButton.addEventListener(MouseEvent.CLICK,onMCButtonClick);
                                  simpleButton.addEventListener(MouseEvent.MOUSE_OVER,onMCButtonClick);

                                  (loadedSWFMainTimeline.button1162 as SimpleButton).loaderInfo
                                  var textSnap:TextSnapshot = loadedSWFMainTimeline.textSnapshot;
                                 var str:String =  textSnap.getText(0,textSnap.charCount,true);
                      // To make the things and ID capturing dynamic i had tried following way      

                                   /* if(loadedSWFMainTimeline)
                                   {
                                    for each (var obj:String in loadedSWFMainTimeline)
                                    {
                                        if(loadedSWFMainTimeline[obj] is SimpleButton)
                                        {
                                            Alert.show(ObjectUtil.toString(obj));
                                        }   
                                    } */

                       

                           } 

                         public function onMCButtonClick(event:MouseEvent):void {
                                  if(loadedSWFMainTimeline)
                                  {
                                   Alert.show("HELLO SWF LINK CLICKED");
                                  }   

                       

                      But neither of above works, Would appreciate any comment on your part.

                       

                      Thanks.

                      • 8. Re: Click event on SWFLoader
                        BhaskerChari Level 4

                        Hi SRKKing,

                         

                        Can you mail me the sample SWF file you are working with....??? So that way I can help you in figuring out your problem..exactly...!!!

                         

                        Also please mention on which link you want the event to captured...

                         

                         

                        Thanks,

                        Bhasker

                        • 9. Re: Click event on SWFLoader
                          shashank Kulkarni Level 1

                          HI Bhaskar,

                           

                          Thanks for your valuable time and comments. Indeed it definatly worked with inputs.

                           

                          loader.root.addEventListener(MouseEvent.CLICK, onMCButtonClick);

                           

                          has started working for me and now i just need to put if condition for my desired target.

                           

                          Thanks and Regards,

                          Shashank