0 Replies Latest reply on Jan 15, 2007 9:21 PM by KnowRama

    Developer Tip: Easy Event Creation in AS 2

    KnowRama
      You can ignore the Event Dispatcher class to create custom events.( if you want to )
      ------
      This tip is for those who ever wanted some custom event that would be bound to their custom class like the one shown below ( bcos I wanted one like this )
      i.e. Implementing a function outside a class..( or overriding a function outside a class to simulate an event )

      myClass.onEventName=function (){
      // some block of code..
      }

      Read the following code.. Below is a TimeOut class that would fire an event ( a function ) called "onTimeOut"

      class TimeOut{

      public var onTimeOut:Function ;

      var endTime:Number;
      var IntID:Number;

      function TimeOut(secs :Number){

      endTime=(secs*1000) +getTimer();
      IntID = setInterval(this,"checkTime",50)

      }

      function checkTime (){

      if (endTime > getTimer()){

      }else {
      this.onTimeOut() // event fired from the here
      clearInterval(IntID);
      }
      }
      }
      ------------
      The above class can be implemented in your movie like this..

      var myTimeOut=new TimeOut(3);

      myTimeOut.onTimeOut=function (){
      trace("timeout fired ");
      }

      To achieve this appears to be simple and I was really wondering why the Flash Development team did not encourage this approach instead of the Event Dispatcher class.

      Is there any specific reason for using the Event Dispatcher class ( perhaps a design strategy ) . Flash Gurus or the Tech Team could throw more light on this.

      -- KM
      P.S: For better readability copy the script to your AS editor.