2 Replies Latest reply on Jan 18, 2011 3:13 PM by jordana309-36

    Catching Custom Events in Actionscript

    jordana309-36

      I have a custom component that opens a new window with a video player in it. I need to pass a bunch of stuff into it, so there's a lot of binding expressions. I need to be able to catch it's complete event (when the video is finished playing). But the event I dispatch isn't working. Here's my custom component class:

       

      [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;
                  // In order to allow it to play, I had to extend the play method for the VideoDisplay to a public function.
                  public function play():void
                  {
                      videoDisplay.play();
                  }
                 
                  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="videoDisplay" x="0" y="0" width="{displayScreenWidth}" height="{displayScreenHeight}" source="videos/{videoName}"
                                          autoPlay="true" muted="{muteVideo}" loop="{loopVideo}" complete="dispatchMovieFinish(event)" />
      </s:Window>

      [/CODE]

       

      When I try to catch the event, I use this code:

      [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.MOVIE_FINISH, loadScreenSaver);
          videoScreen.muteVideo = false;
          videoScreen.loopVideo = false;
          videoScreen.videoName = vidName;
          videoScreen.play();
      }

      [/CODE]

       

      The problem is, i get the error by the addEventListener line that reads:

      Multiple markers at this line:
      -1119: Access of possibly undefined property MOVIE_FINISH through a reference with static type Class.
      -addEventListener

       

      I don't know how to get around this. When I type "addEventListener(" and hit control+space bar, I see "Event.MOVIE_FINISH" on the list, citing my custom component as the source. Even when I had it called movieFinish in the component, I still saw "MOVIE_FINISH" for my event in the code help. So, I changed the name, but I cannot get rid of the error, and therefore my program won't compile fully. Any ideas?