5 Replies Latest reply on Oct 28, 2009 5:49 PM by arby10101

    Handling custom event dispatched by loaded SWF fails

    arby10101 Level 1

      I have loaded and SWF using SWFLoader. Not a component, but just a regular Flash AS3 SWF. I can access its properties and functions but when I listen for a custom event it dispatches, I get the error:

       

      Error #1034: Type Coercion failed: cannot convert com.subhumanmedia.classes::SoundCueEvent@911ad61 to com.subhumanmedia.classes.SoundCueEvent.

       

      Here is the event:
      package com.subhumanmedia.classes
      {
      import flash.events.Event;
      public class SoundCueEvent extends Event
      {
          public static const CUE_SOUND:String = "cueSound";
         
          private var soundCueObjectInstance:Object;
         
          public function SoundCueEvent(type:String, bubbles:Boolean, cancelable:Boolean, soundCueObject:Object)
          {
              // soundCueObject parameter:
              //        soundCueObject.cueSoundURL is String with the filename to play
              //        soundCueObject.cueSoundStartTime is Number giving the millisecond offset to play from
              //
              super(type, bubbles, cancelable);
              soundCueObjectInstance = soundCueObject;
          }
          public function get soundCueObject():Object
          {
             return soundCueObjectInstance;
          }
          override public function clone():Event
          {
             return new SoundCueEvent(type, bubbles, cancelable, soundCueObjectInstance);
          }
      }
      }

       

      Here is the statement in the loaded SWF dispatching the event:
      dispatchEvent(new SoundCueEvent(SoundCueEvent.CUE_SOUND, true, false, soundCues[cueCounter]));


      soundCues is an attay of soundCueObjects.

       

      Here is the listener definition in my main class:
      slide.slideGraphic.swfObject.content.addEventListener(SoundCueEvent.CUE_SOUND, cueSoundHandler);

       

      And here is the handler:
      private function cueSoundHandler(e:SoundCueEvent):void
      {
          trace(e);
      }

       

      If I change the handler parameter type to Event, it doesn't throw an error, but I still can't cast the Event to a SoundCueEvent to be able to get at the cue information.

       

      Right now both the loaded SWF and my main class are getting the event definition from a compiled SWC, but I've tried several other ways of doing this with no better results.

       

      I don't even know what's causing the error and am totally stumped by how to solve it. Can anyone help???

        • 1. Re: Handling custom event dispatched by loaded SWF fails
          Flex harUI Adobe Employee

          That's the equivalent of the shared code problem described in the modules presentation on my blog.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Handling custom event dispatched by loaded SWF fails
            arby10101 Level 1

            Thanks for the reply, Alex. But I read the blog three more times (and had already read it before) and couldn't identify the part that could point to an answer to my problem. Could you be more specific? Thanks!

            • 3. Re: Handling custom event dispatched by loaded SWF fails
              arby10101 Level 1

              Possibly I found the blog section you're referring to--the 360 Flex Conference.

               

              The solution there, if I understand it, is to put shared code in a class that extends ModuleBase, and then use ModuleManager and IModuleInfo to load that class (compiled to a SWF) at startup into the main application. Is that correct?

               

              Is that really the only way to catch a custom event from a loaded SWF? I'm not creating the SWFs that are loaded. Many of them will be dirt simple, even InDesign or Illustrator pages saved as SWF. But some content will be considerably more elaborate. And none are likely to be even remotely similar to a module. They're going to be timeline heavy with some AS3.

               

              I provided an IControllableSWF interface to let Flash developers make their SWF accessible to my application. This was chiefly to overcome problems caused by a loaded SWF being unable to access the Stage. I also provided the SoundCueEvent with a callback when the sound has completed playing. This allows Flash developers to synch voiceovers without having to use dozens of timers, empty frames, etc.

               

              I really would like to keep it as simple as possible for the people making the content SWFs.

               

              Of course, maybe I didn't even find the right reference that you meant, in which case--sorry.

              • 4. Re: Handling custom event dispatched by loaded SWF fails
                Flex harUI Adobe Employee

                Yeah, that's the one.  The classes SWFs have in common need to be in the top-level application domain.

                 

                Another option is to use MarshallPlan techniques but that might not be possible if you don't own the SWFs.  There is a Marshall Plan post on my blog and more info on the opensource wiki.  One of the key aspects is that our listeners are not strongly typed.  They are typed as flash.events.Event then we map the event to an event in our applicationDomain if needed.

                 

                Alex Harui

                Flex SDK Developer

                Adobe Systems Inc.

                Blog: http://blogs.adobe.com/aharui

                • 5. Re: Handling custom event dispatched by loaded SWF fails
                  arby10101 Level 1

                  Thanks for the answer. Since one goal was to make things as simple as possible for the content developers, I decided to ditch the custom event and get them to use a normal flash Event. I added getters to the content interface so my main class can find the current sound url to play and the current start point. Thanks again for the help.