home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / zombietypocalypse.swf / scripts / fl / transitions / Tween.as next >
Encoding:
Text File  |  2008-09-15  |  8.3 KB  |  324 lines

  1. package fl.transitions
  2. {
  3.    import flash.display.MovieClip;
  4.    import flash.events.Event;
  5.    import flash.events.EventDispatcher;
  6.    import flash.events.TimerEvent;
  7.    import flash.utils.Timer;
  8.    import flash.utils.getTimer;
  9.    
  10.    public class Tween extends EventDispatcher
  11.    {
  12.       
  13.       protected static var _mc:MovieClip = new MovieClip();
  14.        
  15.       
  16.       private var _position:Number = NaN;
  17.       
  18.       public var prevTime:Number = NaN;
  19.       
  20.       public var prevPos:Number = NaN;
  21.       
  22.       public var isPlaying:Boolean = false;
  23.       
  24.       public var begin:Number = NaN;
  25.       
  26.       private var _fps:Number = NaN;
  27.       
  28.       private var _time:Number = NaN;
  29.       
  30.       public var change:Number = NaN;
  31.       
  32.       private var _finish:Number = NaN;
  33.       
  34.       public var looping:Boolean = false;
  35.       
  36.       private var _intervalID:uint = 0;
  37.       
  38.       public var func:Function;
  39.       
  40.       private var _timer:Timer = null;
  41.       
  42.       private var _startTime:Number = NaN;
  43.       
  44.       public var prop:String = "";
  45.       
  46.       private var _duration:Number = NaN;
  47.       
  48.       public var obj:Object = null;
  49.       
  50.       public var useSeconds:Boolean = false;
  51.       
  52.       public function Tween(param1:Object, param2:String, param3:Function, param4:Number, param5:Number, param6:Number, param7:Boolean = false)
  53.       {
  54.          isPlaying = false;
  55.          obj = null;
  56.          prop = "";
  57.          func = function(param1:Number, param2:Number, param3:Number, param4:Number):Number
  58.          {
  59.             return param3 * param1 / param4 + param2;
  60.          };
  61.          begin = NaN;
  62.          change = NaN;
  63.          useSeconds = false;
  64.          prevTime = NaN;
  65.          prevPos = NaN;
  66.          looping = false;
  67.          _duration = NaN;
  68.          _time = NaN;
  69.          _fps = NaN;
  70.          _position = NaN;
  71.          _startTime = NaN;
  72.          _intervalID = 0;
  73.          _finish = NaN;
  74.          _timer = null;
  75.          super();
  76.          if(!arguments.length)
  77.          {
  78.             return;
  79.          }
  80.          this.obj = param1;
  81.          this.prop = param2;
  82.          this.begin = param4;
  83.          this.position = param4;
  84.          this.duration = param6;
  85.          this.useSeconds = param7;
  86.          if(param3 is Function)
  87.          {
  88.             this.func = param3;
  89.          }
  90.          this.finish = param5;
  91.          this._timer = new Timer(100);
  92.          this.start();
  93.       }
  94.       
  95.       public function continueTo(param1:Number, param2:Number) : void
  96.       {
  97.          this.begin = this.position;
  98.          this.finish = param1;
  99.          if(!isNaN(param2))
  100.          {
  101.             this.duration = param2;
  102.          }
  103.          this.start();
  104.       }
  105.       
  106.       public function stop() : void
  107.       {
  108.          this.stopEnterFrame();
  109.          this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_STOP,this._time,this._position));
  110.       }
  111.       
  112.       private function fixTime() : void
  113.       {
  114.          if(this.useSeconds)
  115.          {
  116.             this._startTime = getTimer() - this._time * 1000;
  117.          }
  118.       }
  119.       
  120.       public function set FPS(param1:Number) : void
  121.       {
  122.          var _loc2_:Boolean = false;
  123.          _loc2_ = this.isPlaying;
  124.          this.stopEnterFrame();
  125.          this._fps = param1;
  126.          if(_loc2_)
  127.          {
  128.             this.startEnterFrame();
  129.          }
  130.       }
  131.       
  132.       public function get finish() : Number
  133.       {
  134.          return this.begin + this.change;
  135.       }
  136.       
  137.       public function get duration() : Number
  138.       {
  139.          return this._duration;
  140.       }
  141.       
  142.       protected function startEnterFrame() : void
  143.       {
  144.          var _loc1_:Number = NaN;
  145.          if(isNaN(this._fps))
  146.          {
  147.             _mc.addEventListener(Event.ENTER_FRAME,this.onEnterFrame,false,0,true);
  148.          }
  149.          else
  150.          {
  151.             _loc1_ = 1000 / this._fps;
  152.             this._timer.delay = _loc1_;
  153.             this._timer.addEventListener(TimerEvent.TIMER,this.timerHandler,false,0,true);
  154.             this._timer.start();
  155.          }
  156.          this.isPlaying = true;
  157.       }
  158.       
  159.       public function set time(param1:Number) : void
  160.       {
  161.          this.prevTime = this._time;
  162.          if(param1 > this.duration)
  163.          {
  164.             if(this.looping)
  165.             {
  166.                this.rewind(param1 - this._duration);
  167.                this.update();
  168.                this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_LOOP,this._time,this._position));
  169.             }
  170.             else
  171.             {
  172.                if(this.useSeconds)
  173.                {
  174.                   this._time = this._duration;
  175.                   this.update();
  176.                }
  177.                this.stop();
  178.                this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_FINISH,this._time,this._position));
  179.             }
  180.          }
  181.          else if(param1 < 0)
  182.          {
  183.             this.rewind();
  184.             this.update();
  185.          }
  186.          else
  187.          {
  188.             this._time = param1;
  189.             this.update();
  190.          }
  191.       }
  192.       
  193.       protected function stopEnterFrame() : void
  194.       {
  195.          if(isNaN(this._fps))
  196.          {
  197.             _mc.removeEventListener(Event.ENTER_FRAME,this.onEnterFrame);
  198.          }
  199.          else
  200.          {
  201.             this._timer.stop();
  202.          }
  203.          this.isPlaying = false;
  204.       }
  205.       
  206.       public function getPosition(param1:Number = NaN) : Number
  207.       {
  208.          if(isNaN(param1))
  209.          {
  210.             param1 = this._time;
  211.          }
  212.          return this.func(param1,this.begin,this.change,this._duration);
  213.       }
  214.       
  215.       public function set finish(param1:Number) : void
  216.       {
  217.          this.change = param1 - this.begin;
  218.       }
  219.       
  220.       public function set duration(param1:Number) : void
  221.       {
  222.          this._duration = param1 <= 0 ? Infinity : param1;
  223.       }
  224.       
  225.       public function setPosition(param1:Number) : void
  226.       {
  227.          this.prevPos = this._position;
  228.          if(this.prop.length)
  229.          {
  230.             this.obj[this.prop] = this._position = param1;
  231.          }
  232.          this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_CHANGE,this._time,this._position));
  233.       }
  234.       
  235.       public function resume() : void
  236.       {
  237.          this.fixTime();
  238.          this.startEnterFrame();
  239.          this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_RESUME,this._time,this._position));
  240.       }
  241.       
  242.       public function fforward() : void
  243.       {
  244.          this.time = this._duration;
  245.          this.fixTime();
  246.       }
  247.       
  248.       protected function onEnterFrame(param1:Event) : void
  249.       {
  250.          this.nextFrame();
  251.       }
  252.       
  253.       public function get position() : Number
  254.       {
  255.          return this.getPosition(this._time);
  256.       }
  257.       
  258.       public function yoyo() : void
  259.       {
  260.          this.continueTo(this.begin,this.time);
  261.       }
  262.       
  263.       public function nextFrame() : void
  264.       {
  265.          if(this.useSeconds)
  266.          {
  267.             this.time = (getTimer() - this._startTime) / 1000;
  268.          }
  269.          else
  270.          {
  271.             this.time = this._time + 1;
  272.          }
  273.       }
  274.       
  275.       protected function timerHandler(param1:TimerEvent) : void
  276.       {
  277.          this.nextFrame();
  278.          param1.updateAfterEvent();
  279.       }
  280.       
  281.       public function get FPS() : Number
  282.       {
  283.          return this._fps;
  284.       }
  285.       
  286.       public function rewind(param1:Number = 0) : void
  287.       {
  288.          this._time = param1;
  289.          this.fixTime();
  290.          this.update();
  291.       }
  292.       
  293.       public function set position(param1:Number) : void
  294.       {
  295.          this.setPosition(param1);
  296.       }
  297.       
  298.       public function get time() : Number
  299.       {
  300.          return this._time;
  301.       }
  302.       
  303.       private function update() : void
  304.       {
  305.          this.setPosition(this.getPosition(this._time));
  306.       }
  307.       
  308.       public function start() : void
  309.       {
  310.          this.rewind();
  311.          this.startEnterFrame();
  312.          this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_START,this._time,this._position));
  313.       }
  314.       
  315.       public function prevFrame() : void
  316.       {
  317.          if(!this.useSeconds)
  318.          {
  319.             this.time = this._time - 1;
  320.          }
  321.       }
  322.    }
  323. }
  324.