home *** CD-ROM | disk | FTP | other *** search
/ Mobiclic 161 / MOBICLIC161.ISO / pc / DATA / DSS161 / DSS161_00 / DSS161_00.swf / scripts / dss161 / engineaddons / gamesprotos / actors / actorClip / ActorTimer.as < prev    next >
Text File  |  2014-01-15  |  5KB  |  175 lines

  1. package dss161.engineaddons.gamesprotos.actors.actorClip
  2. {
  3.    import dss161.engineaddons.facades.GameEngine;
  4.    import dss161.engineaddons.gamesprotos.Actor;
  5.    import flash.events.TimerEvent;
  6.    import flash.utils.Timer;
  7.    import flash.utils.getTimer;
  8.    
  9.    public class ActorTimer extends Actor
  10.    {
  11.        
  12.       
  13.       protected var timer:Timer;
  14.       
  15.       protected var _duration:Number = 999999999;
  16.       
  17.       protected var _delay:Number = 10;
  18.       
  19.       protected var _repeatCount:Number = 0;
  20.       
  21.       protected var _callback:Function = null;
  22.       
  23.       protected var _onTic:Function = null;
  24.       
  25.       protected var timeStart:Number = 0;
  26.       
  27.       protected var timeLast:Number = 0;
  28.       
  29.       protected var timerToWake:Boolean = false;
  30.       
  31.       public function ActorTimer(param1:GameEngine, param2:*)
  32.       {
  33.          super(param1,null,param2);
  34.          if(param2.duration != undefined)
  35.          {
  36.             this._duration = param2.duration;
  37.          }
  38.          if(param2.delay != undefined)
  39.          {
  40.             this._delay = param2.delay;
  41.          }
  42.          if(param2.callback != undefined)
  43.          {
  44.             this._callback = param2.callback;
  45.          }
  46.          if(param2.onTic != undefined)
  47.          {
  48.             this._onTic = param2.onTic;
  49.          }
  50.          this.timer = new Timer(this._delay,this._repeatCount);
  51.       }
  52.       
  53.       public static function create(param1:GameEngine, param2:*) : ActorTimer
  54.       {
  55.          return new ActorTimer(param1,param2);
  56.       }
  57.       
  58.       protected function timerEventTic(param1:TimerEvent) : void
  59.       {
  60.          this._onTic(this);
  61.       }
  62.       
  63.       protected function timerEventDuration(param1:TimerEvent) : void
  64.       {
  65.          this.timeLast = getTimer();
  66.          if(this.timeLast - this.timeStart > this._duration)
  67.          {
  68.             this.stop();
  69.             this._callback(this);
  70.          }
  71.       }
  72.       
  73.       override protected function _sleep() : void
  74.       {
  75.          super._sleep();
  76.          if(isSleeping)
  77.          {
  78.             return;
  79.          }
  80.          isSleeping = true;
  81.          if(this.timer.running)
  82.          {
  83.             this.timerToWake = true;
  84.             this.timer.stop();
  85.          }
  86.       }
  87.       
  88.       override protected function _wake() : void
  89.       {
  90.          var _loc1_:Number = NaN;
  91.          super._wake();
  92.          if(isSleeping = false)
  93.          {
  94.             return;
  95.          }
  96.          isSleeping = false;
  97.          if(this.timerToWake)
  98.          {
  99.             this.timerToWake = false;
  100.             _loc1_ = getTimer() - this.timeLast;
  101.             this.timeStart += _loc1_;
  102.             this.timeLast += _loc1_;
  103.             this.timer.start();
  104.          }
  105.       }
  106.       
  107.       override protected function _destroy() : void
  108.       {
  109.          super._destroy();
  110.          if(this.timer != null)
  111.          {
  112.             this.stop();
  113.             this.timer = null;
  114.             this._callback = null;
  115.          }
  116.       }
  117.       
  118.       public function get currentCount() : int
  119.       {
  120.          return this.timer.currentCount;
  121.       }
  122.       
  123.       public function get delay() : Number
  124.       {
  125.          return this.timer.delay;
  126.       }
  127.       
  128.       public function set delay(param1:Number) : void
  129.       {
  130.          this.timer.delay = param1;
  131.       }
  132.       
  133.       public function get repeatCount() : int
  134.       {
  135.          return this.timer.repeatCount;
  136.       }
  137.       
  138.       public function set repeatCount(param1:int) : void
  139.       {
  140.          this.timer.repeatCount = param1;
  141.       }
  142.       
  143.       public function reset() : void
  144.       {
  145.          this.timer.reset();
  146.       }
  147.       
  148.       public function get running() : Boolean
  149.       {
  150.          return this.timer.running;
  151.       }
  152.       
  153.       override public function start() : void
  154.       {
  155.          if(this._onTic != null)
  156.          {
  157.             this.timer.addEventListener(TimerEvent.TIMER,this.timerEventTic);
  158.          }
  159.          else if(this._callback != null)
  160.          {
  161.             this.timer.addEventListener(TimerEvent.TIMER,this.timerEventDuration);
  162.          }
  163.          this.timeStart = getTimer();
  164.          this.timer.start();
  165.       }
  166.       
  167.       public function stop() : void
  168.       {
  169.          this.timer.removeEventListener(TimerEvent.TIMER,this.timerEventTic);
  170.          this.timer.removeEventListener(TimerEvent.TIMER,this.timerEventDuration);
  171.          this.timer.stop();
  172.       }
  173.    }
  174. }
  175.