home *** CD-ROM | disk | FTP | other *** search
/ Mobiclic 161 / MOBICLIC161.ISO / pc / DATA / DSS161 / DSS161_00 / DSS161_00.swf / scripts / dss161 / engineaddons / timers / MainTimer.as next >
Text File  |  2014-01-15  |  5KB  |  198 lines

  1. package dss161.engineaddons.timers
  2. {
  3.    import flash.events.TimerEvent;
  4.    import flash.utils.Timer;
  5.    import flash.utils.getTimer;
  6.    
  7.    public class MainTimer
  8.    {
  9.        
  10.       
  11.       protected var _timer:Timer;
  12.       
  13.       protected var _delay:Number;
  14.       
  15.       protected var _repeatCount:Number;
  16.       
  17.       protected var _isSleeping:Boolean = false;
  18.       
  19.       protected var _listeners:Vector.<TimerChild>;
  20.       
  21.       protected var _listenersLength:int = 0;
  22.       
  23.       protected var _running:Boolean = false;
  24.       
  25.       protected var _idUnique:int = 1;
  26.       
  27.       public var time:Number = 0;
  28.       
  29.       protected var _tempChild:TimerChild;
  30.       
  31.       protected var timerToWake:Boolean = false;
  32.       
  33.       public function MainTimer()
  34.       {
  35.          this._timer = new Timer(1);
  36.          this._listeners = new Vector.<TimerChild>();
  37.          super();
  38.       }
  39.       
  40.       public function addFromObj(param1:Object) : TimerChild
  41.       {
  42.          var _loc2_:TimerChild = new TimerChild(this);
  43.          ++this._idUnique;
  44.          _loc2_.id = param1.id == undefined ? "" : param1.id + this._idUnique;
  45.          _loc2_.delay = param1.delay;
  46.          _loc2_.loop = param1.loop == undefined ? 0 : int(param1.loop);
  47.          _loc2_.func = param1.onDelay;
  48.          _loc2_._running = false;
  49.          return _loc2_;
  50.       }
  51.       
  52.       public function add(param1:TimerChild) : void
  53.       {
  54.          if(this._listeners.indexOf(param1) == -1)
  55.          {
  56.             this._listeners.push(param1);
  57.             this._listenersLength = this._listeners.length;
  58.          }
  59.          this.start();
  60.       }
  61.       
  62.       public function removeAllChilds() : void
  63.       {
  64.          this._listeners.length = this._listenersLength = 0;
  65.       }
  66.       
  67.       public function removeChild(param1:TimerChild) : void
  68.       {
  69.          if(this._listeners.indexOf(param1) != -1)
  70.          {
  71.             this._listeners.splice(this._listeners.indexOf(param1),1);
  72.             this._listenersLength = this._listeners.length;
  73.          }
  74.       }
  75.       
  76.       public function start() : void
  77.       {
  78.          if(this._running)
  79.          {
  80.             return;
  81.          }
  82.          this._running = true;
  83.          this._timer.addEventListener(TimerEvent.TIMER,this._timeHandler);
  84.          this.resetAll();
  85.          this._timer.start();
  86.       }
  87.       
  88.       public function resetAll() : void
  89.       {
  90.          for each(this._tempChild in this._listeners)
  91.          {
  92.             this._tempChild.timeStart = getTimer();
  93.             this._tempChild.timeElapsed = 0;
  94.          }
  95.       }
  96.       
  97.       public function stopAllChildren() : void
  98.       {
  99.          for each(this._tempChild in this._listeners)
  100.          {
  101.             this._tempChild.stop();
  102.          }
  103.       }
  104.       
  105.       protected function _timeHandler(param1:TimerEvent) : void
  106.       {
  107.          this.time = getTimer();
  108.          for each(this._tempChild in this._listeners)
  109.          {
  110.             if(this._running == false)
  111.             {
  112.                return;
  113.             }
  114.             if((this._tempChild.timeElapsed = this.time - this._tempChild.timeStart) > this._tempChild.delay)
  115.             {
  116.                this._tempChild.timeStart = this.time;
  117.                this._tempChild.timeElapsed = 0;
  118.                this._tempChild.func(this._tempChild);
  119.                if(this._tempChild.loop > 1)
  120.                {
  121.                   --this._tempChild.loop;
  122.                }
  123.                else if(this._tempChild.loop == 1)
  124.                {
  125.                   this._tempChild.stop();
  126.                }
  127.             }
  128.          }
  129.          if(this._listeners.length == 0)
  130.          {
  131.             this.stop();
  132.          }
  133.          param1.updateAfterEvent();
  134.       }
  135.       
  136.       public function get numChildren() : int
  137.       {
  138.          return this._listenersLength;
  139.       }
  140.       
  141.       public function get running() : Boolean
  142.       {
  143.          return this._running;
  144.       }
  145.       
  146.       public function stop() : void
  147.       {
  148.          if(this._running == false)
  149.          {
  150.             return;
  151.          }
  152.          this._running = false;
  153.          this._timer.removeEventListener(TimerEvent.TIMER,this._timeHandler);
  154.          this._timer.stop();
  155.       }
  156.       
  157.       public function sleep() : void
  158.       {
  159.          if(this._isSleeping)
  160.          {
  161.             return;
  162.          }
  163.          this._isSleeping = true;
  164.          if(this._timer.running)
  165.          {
  166.             this.timerToWake = true;
  167.             this.stop();
  168.          }
  169.          this.stop();
  170.       }
  171.       
  172.       public function wake() : void
  173.       {
  174.          var offsetTime:Number = NaN;
  175.          if(this._isSleeping == false)
  176.          {
  177.             return;
  178.          }
  179.          this._isSleeping = false;
  180.          if(this.timerToWake)
  181.          {
  182.             offsetTime = getTimer() - this.time;
  183.             this._listeners.forEach(function(param1:*, param2:int, param3:Vector.<TimerChild>):void
  184.             {
  185.                param1.timeStart + offsetTime;
  186.             },this);
  187.             this.start();
  188.          }
  189.       }
  190.       
  191.       public function destroy() : void
  192.       {
  193.          this.stop();
  194.          this.removeAllChilds();
  195.       }
  196.    }
  197. }
  198.