home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Acao / flameout.swf / scripts / __Packages / LRG / Utils / LRGGameTimer.as < prev   
Encoding:
Text File  |  2006-06-13  |  2.4 KB  |  95 lines

  1. class LRG.Utils.LRGGameTimer extends MovieClip
  2. {
  3.    var m_fOffsetTime;
  4.    var m_fStartedTime;
  5.    var m_fStoppedTime;
  6.    var m_fCurrentTime;
  7.    var m_pClockListeners;
  8.    var m_fIntervalId;
  9.    var m_fTimeToRun;
  10.    function LRGGameTimer()
  11.    {
  12.       super();
  13.       this.init();
  14.    }
  15.    function init()
  16.    {
  17.       this.m_fOffsetTime = 0;
  18.       this.m_fStartedTime = undefined;
  19.       this.m_fStoppedTime = undefined;
  20.       this.m_fCurrentTime = undefined;
  21.       this.m_pClockListeners = new Array();
  22.       this.m_fIntervalId = undefined;
  23.       this.m_fTimeToRun = Infinity;
  24.    }
  25.    function getTime()
  26.    {
  27.       return this.m_fCurrentTime;
  28.    }
  29.    function isRunning()
  30.    {
  31.       return this.m_fIntervalId != undefined;
  32.    }
  33.    function stopClock()
  34.    {
  35.       if(this.isRunning())
  36.       {
  37.          clearInterval(this.m_fIntervalId);
  38.          this.m_fIntervalId = undefined;
  39.          this.m_fStoppedTime = getTimer() / 1000;
  40.          var _loc2_ = 0;
  41.          while(_loc2_ < this.m_pClockListeners.length)
  42.          {
  43.             this.m_pClockListeners[_loc2_].doOnClockStopped(this);
  44.             _loc2_ = _loc2_ + 1;
  45.          }
  46.       }
  47.    }
  48.    function startClock(bReset)
  49.    {
  50.       if(bReset || this.m_fCurrentTime == undefined)
  51.       {
  52.          this.resetClock();
  53.       }
  54.       else
  55.       {
  56.          this.m_fOffsetTime += getTimer() / 1000 - this.m_fStoppedTime;
  57.          this.m_fCurrentTime = getTimer() / 1000 - this.m_fOffsetTime;
  58.       }
  59.       var _loc2_ = 0;
  60.       while(_loc2_ < this.m_pClockListeners.length)
  61.       {
  62.          this.m_pClockListeners[_loc2_].doOnClockStarted(this);
  63.          _loc2_ = _loc2_ + 1;
  64.       }
  65.       this.m_fIntervalId = setInterval(this,"updateClock",8.333333333333334);
  66.    }
  67.    function resetClock()
  68.    {
  69.       this.m_fCurrentTime = 0;
  70.       this.m_fOffsetTime = getTimer() / 1000;
  71.    }
  72.    function setTimeToRun(fTime)
  73.    {
  74.       this.m_fTimeToRun = fTime;
  75.    }
  76.    function addListener(kListener)
  77.    {
  78.       this.m_pClockListeners.push(kListener);
  79.    }
  80.    function removeListener(kListener)
  81.    {
  82.       var _loc2_ = this.m_pClockListeners;
  83.       this.m_pClockListeners = _loc2_.removeData(kListener);
  84.       false;
  85.    }
  86.    function updateClock()
  87.    {
  88.       this.m_fCurrentTime = getTimer() / 1000 - this.m_fOffsetTime;
  89.       if(this.m_fCurrentTime >= this.m_fTimeToRun)
  90.       {
  91.          this.stopClock();
  92.       }
  93.    }
  94. }
  95.