0 Replies Latest reply on Jan 14, 2011 9:19 PM by jordana309-36

    Catching Custom Events in Actionscript

    jordana309-36

      I have a component that contains a VideoDisplay object. I need my main application to know when it's reached the end of it's playing. I also pass a mess of things into this from my primary window, so there's a lot of bindable variables. So, I currently have the following code in my object:

       

      [CODE]

      <?xml version="1.0" encoding="utf-8"?>
      <!-- This is a window component which creates a transparent window with no system chrome (no bars, close, min, max buttons, resize stuff) that contains a single video object to be able to play videos from a folder called "videos" in the root of this application. This screen is spawned from the main MXML WindowedApplication using AS 3. -->
      <s:Window 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:customComponents="customComponents.*"
                          systemChrome="none" visible="true" transparent="true" showStatusBar="false" width="400" height="300"
                          >
          <fx:Metadata>
              [Event(name="MOVIE_LOAD", type="flash.events.Event")]
              [Event(name="MOVIE_FINISH", type="flash.events.Event")]
          </fx:Metadata>
         
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
         
          <s:layout>
              <s:BasicLayout />
          </s:layout>
         
          <fx:Script>
              <![CDATA[
                  import org.osmf.events.TimeEvent;
                  [Bindable]
                  public var displayScreenWidth:int = 1024;
                  [Bindable]
                  public var displayScreenHeight:int = 768;
                  [Bindable]
                  public var videoName:String;
                  // Note that the following two variables are set to true by default because the screen saver goes in first.
                  [Bindable]
                  public var muteVideo:Boolean=true;
                  [Bindable]
                  public var loopVideo:Boolean=true;
                 
                  protected function dispatchMovieLoad(event:Event):void
                  {
                      var eventObj:Event = new Event("MOVIE_LOAD");
                      dispatchEvent(eventObj);
                  }
                  protected function dispatchMovieFinish(event:TimeEvent):void
                  {
                      var eventObj:Event = new Event("MOVIE_FINISH");
                      dispatchEvent(eventObj);
                  }

       

              ]]>
          </fx:Script>
         
          <s:VideoDisplay id="videoDisplayComp" x="0" y="0" width="{displayScreenWidth}" height="{displayScreenHeight}" source="videos/{videoName}"
                                          autoPlay="true" muted="{muteVideo}" loop="{loopVideo}" complete="dispatchMovieFinish(event)" />
      </s:Window>

      {/CODE]

       

      Anyway, when I try to catch the event the event in my main screen's actionscript, I have the following:

       

      [CODE]

      /* Plays a video, and enables sound and disables looping. It also defines what do to when the video reaches completion. */
      protected function playVideo(vidName:String):void
      {
          videoScreen.addEventListener(Event.moviefinish, loadScreenSaver);
          videoScreen.muteVideo = false;
          videoScreen.loopVideo = false;
          videoScreen.videoName = vidName;
          videoScreen.videoDisplayComp.play();
      }

      [/CODE]

       

      Now, two things to note. I had to go and access the play() method of the videoDisplay. If I can make this a public function, that'd be awesome.