5 Replies Latest reply on Aug 6, 2010 11:17 AM by Deepanjan Das

    Event Dispatcher Question...

    jerry98225 Level 1

      Hi guys.. I have a question about the event dispatch. I am trying to write the code for youtube player and find the following link..

      http://www.codingcolor.com/as3/as3-youtube-chromless-api/

       

      He has many dispatchEvent call similar like this:

       

       dispatchEvent(new YouTubeEvent(YouTubeEvent.ON_IOERROR,event));

       

      For my understanding, custom event usually dispatch like below:


       dispatchEvent(new Event(YouTubeEvent.ON_IOERROR));

       

      I am not sure why he can add ,event after On_IOERROR. I appreciate if anyone would help me about this. Thanks.

        • 1. Re: Event Dispatcher Question...
          Deepanjan Das Level 3

          Hi,

          Your understanding for event is corrct but not complete.

          The way you showed is a normal event with the type

           

          The event in the question is a custom event YouTubeEvent and passes a parameter that it needs to hold.

           

          Eventually the place where this event is listened to (I mean addEventListened to), needs to get hold of the event.

          This event has nothing to do with YouTubeEvent.

           

          Hope it makes sense.

           

          Warm Regards

          Deepanjan Das

          http://deepanjandas.wordpress.com/

          1 person found this helpful
          • 2. Re: Event Dispatcher Question...
            jerry98225 Level 1

            Thanks Deepanjan Des, I still can't understand your answer....few questions:

             

            1. Where did he create his custom YouTubeEvent, I couldn't find it. All I could find is

             

            dispatchEvent(new YouTubeEvent(YouTubeEvent.ON_IOERROR,event));

             

            I worked on Flex before but we need a metadata to create a custom event.

             

            2. The addEventListener was listening the YouTubeEvent, but why did he pass ,evert as the second argument?

             

            I know I am being a novice...but would appreciate if you can help me again...Thanks!

            • 3. Re: Event Dispatcher Question...
              Deepanjan Das Level 3

              Hi,

              Did you download the source? If you did, then please go to com\YouTube\YouTubeEvent.as

               

               

              It has the following code:

               

              /****************************
              * Manuel Gonzalez           *
              * design@stheory.com        *
              * www.stheory.com           *
              * www.codingcolor.com       *
              *****************************/
              package com.YouTube{
                  import flash.events.Event;

               

                  public class YouTubeEvent extends Event {
                      public var data:*;

               

                      public static  const ON_IOERROR:String = "ON_IOERROR";
                      public static  const ON_CHANGE:String = "ON_CHANGE";
                      public static  const ON_ERROR:String = "ON_ERROR";
                      public static  const ON_READY:String = "ON_READY";
                      public static  const ON_QUALITY_CHANGED:String = "ON_QUALITY_CHANGED";

               

                      public function YouTubeEvent(type:String, data:*) {
                          this.data= data;
                          super(type);
                      }
                  }
              }

               

              So you see this is a custom event and the second parameter passed to the constructor of the event is an untyped data. This means that this event holds differnet data type in data while dispatching different event types as mented in static vars.

               

              Now in flex we used metadata event to access that from its parent.


              If you use as component, you need not write the metadata atg. Instead add the instance using code and add the event listener to it like this:

               

              var myInstance:SomeInstance = new SomeInstance();

              myInstance.addEventListener( MyEvent.TYPE, myEventHandler );

               

              private function myEventHandler( event:MyEvent ):void

              {

                   var obj:* = event.data;//data is the public var in Custom Event class

              }

               

              Warm Regards

              Deepanjan Das

              http://deepanjandas.wordpress.com/

              • 4. Re: Event Dispatcher Question...
                jerry98225 Level 1

                Nice...Thanks a lot !!!!

                • 5. Re: Event Dispatcher Question...
                  Deepanjan Das Level 3

                  Welcome!

                   

                  Warm Regards

                  Deepanjan Das

                  http://deepanjandas.wordpress.com/