14 Replies Latest reply on Mar 24, 2010 5:34 AM by Ibarim

    flex not reading flash event

    Ibarim

      Hi,

       

      I'm importing flash MC into flex. This MC in flash has AS code:

       

      dispatchEvent(new Event('myEvent'));
      

       

      I'm listening to this event in flex with this code:

       

      flashMCinstance.addEventListener('myEvent', myFunction);
      

       

      And it does not work. Is it possible to do it any other way?

        • 1. Re: flex not reading flash event
          jphillipcamp
          • Is your flashMCinstance the Flash MC itself or a wrapper, etc? If it's a Flex MC you need to look to the flashMCinstance.application (you need to make sure the application is loaded first, check for flashMCinstance.application == null first)
          • Does the event bubble?
          • Are you adding the event listener to the right instance?
          • A solution I've used in the past is to define an interface for the parent application.  Both classes would need the same interface definition.  The parent application would implement the interface and call a setParent(this) function on the child where the child sets "this" as the parent reference and call a function on the parent via the interface. something like myParent.myFunction and here you can even pass parameters.  A little more complex than just bubbling up an event but if you are leading a team of developers it provides compile time checking to make sure everything is programed to the contract.  Also it makes sense to use an interface if you will have many types of movie clips that all implement the same functions.
          1 person found this helpful
          • 2. Re: flex not reading flash event
            Ibarim Level 1

            Thnx for reply.

             

            Ad1: my flashMCinstance is not a flex MC, it's imported this way:

             

            //AS code:
            [Embed(source='example.swf', symbol='myMC')]
            [Bindable]
            private var myMC:Class;
            
            //mxml code:
            <mx:SWFLoader id='flashMCinstance' source='{myMC}' />
            
            //AS code:
            flashMCinstance.addEventListener('myEvent', myFunction);
            

             

            So there is no 'flashMCinstance.application' value.

             

            Ad2: I changed dispatcher inside flash 'myMC' movie clip to make it bubble:

             

            dispatchEvent(new Event('myEvent',true));
            

             

            but it didn't help.

             

            Ad3: As you can see in Ad1 the listener is set to the right instance.

             

            Ad4: This is too hard for me, I don't even know how to start to explain how much of this solution I don't understand

            • 3. Re: flex not reading flash event
              AdrianWiecek Level 3

              Hi,

               

              Maybe try to add listener to SWFLoader's content:

               

              flashMCinstance.content.addEventListener('myEvent', myFunction);

               

              Regards,

              Adrian

              • 4. Re: flex not reading flash event
                Ibarim Level 1

                Hi Adrian,

                 

                thnx for help, bu the 'content' didn't work.

                • 5. Re: flex not reading flash event
                  AdrianWiecek Level 3

                  One more idea - add this listener on swfloader complete:

                   

                  <mx:SWFLoader id='flashMCinstance' source='{myMC}' complete="loaderCompleteHandler"/>

                   

                  private function loaderCompleteHandler(event:Event):void

                  {

                       flashMCinstance.content.addEventListener('myEvent', myFunction);

                  }

                   

                  If you dispatch event correctly with bubbling, it should work.

                   

                  Regards,

                  Adrian

                  • 6. Re: flex not reading flash event
                    Ibarim Level 1

                    I used creationComplete instead:

                     

                    <mx:SWFLoader id='flashMCinstance' source='{myMC}' creationComplete="loaderCompleteHandler()"/>
                    
                    private function loaderCompleteHandler():void
                    {
                         flashMCinstance.content.addEventListener('myEvent', myFunction);
                    }
                    

                     

                    and the function executes ok, but listener doesn't 'hear' anything.

                     

                    The dispatcher is like it was previously - with bubbling:

                     

                    dispatchEvent(new Event('myEvent',true));
                    

                     

                    I swear I don't understand why it doesn't work.

                    • 7. Re: flex not reading flash event
                      AdrianWiecek Level 3

                      creationComplete is not the same as complete. creationComplete means that component has finished its construction, property processing, measuring, layout, and drawing. complete means that content loading is complete.

                       

                      Try use debugger and see what type of object gets loaded under content variable.

                       

                      Regards,

                      Adrian

                      • 8. Re: flex not reading flash event
                        Ibarim Level 1

                        I couldn't use 'complete' becouse then the function doesn't execute. For example:

                         

                        <mx:SWFLoader id='flashMCinstance' source='{myMC}' complete="loaderCompleteHandler"/>
                        private function loaderCompleteHandler (event:Event) :void
                        {
                             flashMCinstance.content.addEventListener('myEvent', myFunction);
                        }
                        

                         

                        or

                         

                        <mx:SWFLoader id='flashMCinstance' source='{myMC}' complete="loaderCompleteHandler () "/>
                        private function loaderCompleteHandler () :void
                        {
                             flashMCinstance.content.addEventListener('myEvent', myFunction);
                        }
                        

                         

                        doesn't execute this function, but the creationComplete="loaderCompleteHandler()" with loaderCompleteHandler():void work. I don't know why, becouse I can see the MC on stage loaded, so it should be completed by then.

                         

                         

                        In debugger I see that content variable of this SWFLoader is "nameOfMyApp_myMC" - the 'myMC' is class name for source of this SWFLoader. So it appears that everything is ok, but it still doesn't work.

                        • 9. Re: flex not reading flash event
                          AdrianWiecek Level 3

                          I think now I know why complete is never fired: You are embedding swf first, and use SWFLoader just for display what was embedded already. Try do the same think like this (this way complete also neve fires. Use creationComplete as you've done earlier). At this time I have no other ideas:)

                           

                          <mx:SWFLoader id="flashMCinstance" source="@Embed(source='example.swf', symbol='myMC')"/>


                          • 10. Re: flex not reading flash event
                            Ibarim Level 1

                            I put the source into mxml syntax, function executes, but listener still doesn't work.

                             

                            Thanks for help Adrian (dzięki:)

                             

                            Anybody else has any idea? Please....

                            • 11. Re: flex not reading flash event
                              FTQuest Level 3

                              Adrian has shown you all necessary steps.

                               

                              The only thing that I see as missing is that you don't import your custom event Class in the main application. That is why the function does not recognize the event.

                               

                              HTH,

                              FTQuest

                              • 12. Re: flex not reading flash event
                                Ibarim Level 1

                                Hi FTQuest,

                                 

                                what name the custom event Class has?

                                I was looking in mx.events package for something like:

                                mx.events.Event

                                mx.events.CustomEvent

                                but I can't find it so I don't know which to import.

                                • 13. Re: flex not reading flash event
                                  AdrianWiecek Level 3

                                  Ibarim,

                                   

                                  You are using just an event constant 'myEvent', so there's nothing to import because you didn't create custom event class.

                                   

                                   

                                  Regards,

                                  Adrian

                                  • 14. Re: flex not reading flash event
                                    Ibarim Level 1

                                    Okey,

                                    I found a bypass - I use AS only without MXML and I use 'load' instead of 'source', something like this:

                                     

                                    mySwf = new SWFLoader();
                                    mySwf.load('myFlash.swf');
                                    mySwf.addEventListener(Event.COMPLETE, listen);
                                    
                                    private function listen(event:Event):void {
                                         mySwf.content.addEventListener('myFlashEvent', someFunction);
                                    

                                     

                                    The only problem is that in Flash I had to move MC (with event dispatcher) from library to the stage. Because I don't know how to make reference to specific MC in 'load' method.