home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Puzzle / Easter_Eggs.swf / scripts / __Packages / com / novelgames / flashgames / commonAS2 / NewTimer.as < prev    next >
Text File  |  2008-09-04  |  4KB  |  105 lines

  1. class com.novelgames.flashgames.commonAS2.NewTimer implements com.novelgames.flashgames.commonAS2.Timer
  2. {
  3.    static var paused = false;
  4.    static var totalPausedTime = 0;
  5.    function NewTimer(delay, repeatCount)
  6.    {
  7.       if(repeatCount == undefined)
  8.       {
  9.          repeatCount = 0;
  10.       }
  11.       this.delay = delay;
  12.       this.repeatCount = repeatCount;
  13.       this.currentCount = 0;
  14.    }
  15.    static function getTimer()
  16.    {
  17.       if(com.novelgames.flashgames.commonAS2.NewTimer.paused)
  18.       {
  19.          return com.novelgames.flashgames.commonAS2.NewTimer.pauseTime - com.novelgames.flashgames.commonAS2.NewTimer.totalPausedTime;
  20.       }
  21.       return getTimer() - com.novelgames.flashgames.commonAS2.NewTimer.totalPausedTime;
  22.    }
  23.    static function pause()
  24.    {
  25.       if(com.novelgames.flashgames.commonAS2.NewTimer.paused)
  26.       {
  27.          return undefined;
  28.       }
  29.       com.novelgames.flashgames.commonAS2.NewTimer.paused = true;
  30.       com.novelgames.flashgames.commonAS2.NewTimer.pauseTime = getTimer();
  31.    }
  32.    static function unpause()
  33.    {
  34.       if(!com.novelgames.flashgames.commonAS2.NewTimer.paused)
  35.       {
  36.          return undefined;
  37.       }
  38.       com.novelgames.flashgames.commonAS2.NewTimer.paused = false;
  39.       com.novelgames.flashgames.commonAS2.NewTimer.totalPausedTime += getTimer() - com.novelgames.flashgames.commonAS2.NewTimer.pauseTime;
  40.    }
  41.    function addEventListener(type, listenerObject, listenerFunctionName)
  42.    {
  43.       if(type != com.novelgames.flashgames.commonAS2.TimerEvent.TIMER)
  44.       {
  45.          return undefined;
  46.       }
  47.       this.listenerObject = listenerObject;
  48.       this.listenerFunctionName = listenerFunctionName;
  49.    }
  50.    function removeEventListener(type)
  51.    {
  52.       if(type != com.novelgames.flashgames.commonAS2.TimerEvent.TIMER)
  53.       {
  54.          return undefined;
  55.       }
  56.       this.listenerObject = null;
  57.       this.listenerFunctionName = null;
  58.    }
  59.    function start()
  60.    {
  61.       this.adjustedStartTime = com.novelgames.flashgames.commonAS2.NewTimer.getTimer();
  62.       this.usingDelay = this.delay;
  63.       clearInterval(this.intervalID);
  64.       this.intervalID = setInterval(this,"timerEventListener",this.delay);
  65.    }
  66.    function stop()
  67.    {
  68.       clearInterval(this.intervalID);
  69.    }
  70.    function timerEventListener()
  71.    {
  72.       this.currentCount = this.currentCount + 1;
  73.       if(com.novelgames.flashgames.commonAS2.NewTimer.paused)
  74.       {
  75.          clearInterval(this.intervalID);
  76.          this.usingDelay = Math.max(this.delay - (com.novelgames.flashgames.commonAS2.NewTimer.getTimer() - this.adjustedStartTime),1);
  77.          this.currentCount = this.currentCount - 1;
  78.          this.intervalID = setInterval(this,"timerEventListener",this.usingDelay);
  79.          return undefined;
  80.       }
  81.       if(com.novelgames.flashgames.commonAS2.NewTimer.getTimer() - this.adjustedStartTime >= this.delay)
  82.       {
  83.          this.adjustedStartTime = com.novelgames.flashgames.commonAS2.NewTimer.getTimer();
  84.          if(this.usingDelay != this.delay)
  85.          {
  86.             clearInterval(this.intervalID);
  87.             this.usingDelay = this.delay;
  88.             this.intervalID = setInterval(this,"timerEventListener",this.usingDelay);
  89.          }
  90.          this.listenerObject[this.listenerFunctionName](new com.novelgames.flashgames.commonAS2.TimerEvent());
  91.          if(this.repeatCount != 0 && this.currentCount >= this.repeatCount)
  92.          {
  93.             clearInterval(this.intervalID);
  94.          }
  95.       }
  96.       else
  97.       {
  98.          clearInterval(this.intervalID);
  99.          this.usingDelay = Math.max(this.delay - (com.novelgames.flashgames.commonAS2.NewTimer.getTimer() - this.adjustedStartTime),1);
  100.          this.currentCount = this.currentCount - 1;
  101.          this.intervalID = setInterval(this,"timerEventListener",this.usingDelay);
  102.       }
  103.    }
  104. }
  105.