home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Corrida / drifting.swf / scripts / __Packages / mx / transitions / Tween.as < prev   
Encoding:
Text File  |  2005-08-23  |  5.1 KB  |  243 lines

  1. class mx.transitions.Tween
  2. {
  3.    var obj;
  4.    var prop;
  5.    var begin;
  6.    var useSeconds;
  7.    var _listeners;
  8.    var addListener;
  9.    var prevTime;
  10.    var _time;
  11.    var looping;
  12.    var _duration;
  13.    var broadcastMessage;
  14.    var isPlaying;
  15.    var _fps;
  16.    var prevPos;
  17.    var _pos;
  18.    var change;
  19.    var _intervalID;
  20.    var _startTime;
  21.    static var __initBeacon = mx.transitions.OnEnterFrameBeacon.init();
  22.    static var __initBroadcaster = mx.transitions.BroadcasterMX.initialize(mx.transitions.Tween.prototype,true);
  23.    function Tween(obj, prop, func, begin, finish, duration, useSeconds)
  24.    {
  25.       mx.transitions.OnEnterFrameBeacon.init();
  26.       if(!arguments.length)
  27.       {
  28.          return;
  29.       }
  30.       this.obj = obj;
  31.       this.prop = prop;
  32.       this.begin = begin;
  33.       this.position = begin;
  34.       this.duration = duration;
  35.       this.useSeconds = useSeconds;
  36.       if(func)
  37.       {
  38.          this.func = func;
  39.       }
  40.       this.finish = finish;
  41.       this._listeners = [];
  42.       this.addListener(this);
  43.       this.start();
  44.    }
  45.    function set time(t)
  46.    {
  47.       this.prevTime = this._time;
  48.       if(t > this.duration)
  49.       {
  50.          if(this.looping)
  51.          {
  52.             this.rewind(t - this._duration);
  53.             this.update();
  54.             this.broadcastMessage("onMotionLooped",this);
  55.          }
  56.          else
  57.          {
  58.             if(this.useSeconds)
  59.             {
  60.                this._time = this._duration;
  61.                this.update();
  62.             }
  63.             this.stop();
  64.             this.broadcastMessage("onMotionFinished",this);
  65.          }
  66.       }
  67.       else if(t < 0)
  68.       {
  69.          this.rewind();
  70.          this.update();
  71.       }
  72.       else
  73.       {
  74.          this._time = t;
  75.          this.update();
  76.       }
  77.    }
  78.    function get time()
  79.    {
  80.       return this._time;
  81.    }
  82.    function set duration(d)
  83.    {
  84.       this._duration = !(d == null || d <= 0) ? d : _global.Infinity;
  85.    }
  86.    function get duration()
  87.    {
  88.       return this._duration;
  89.    }
  90.    function set FPS(fps)
  91.    {
  92.       var _loc2_ = this.isPlaying;
  93.       this.stopEnterFrame();
  94.       this._fps = fps;
  95.       if(_loc2_)
  96.       {
  97.          this.startEnterFrame();
  98.       }
  99.    }
  100.    function get FPS()
  101.    {
  102.       return this._fps;
  103.    }
  104.    function set position(p)
  105.    {
  106.       this.setPosition(p);
  107.    }
  108.    function setPosition(p)
  109.    {
  110.       this.prevPos = this._pos;
  111.       this.obj[this.prop] = this._pos = p;
  112.       this.broadcastMessage("onMotionChanged",this,this._pos);
  113.       updateAfterEvent();
  114.    }
  115.    function get position()
  116.    {
  117.       return this.getPosition();
  118.    }
  119.    function getPosition(t)
  120.    {
  121.       if(t == undefined)
  122.       {
  123.          t = this._time;
  124.       }
  125.       return this.func(t,this.begin,this.change,this._duration);
  126.    }
  127.    function set finish(f)
  128.    {
  129.       this.change = f - this.begin;
  130.    }
  131.    function get finish()
  132.    {
  133.       return this.begin + this.change;
  134.    }
  135.    function continueTo(finish, duration)
  136.    {
  137.       this.begin = this.position;
  138.       this.finish = finish;
  139.       if(duration != undefined)
  140.       {
  141.          this.duration = duration;
  142.       }
  143.       this.start();
  144.    }
  145.    function yoyo()
  146.    {
  147.       this.continueTo(this.begin,this.time);
  148.    }
  149.    function startEnterFrame()
  150.    {
  151.       if(this._fps == undefined)
  152.       {
  153.          _global.MovieClip.addListener(this);
  154.       }
  155.       else
  156.       {
  157.          this._intervalID = setInterval(this,"onEnterFrame",1000 / this._fps);
  158.       }
  159.       this.isPlaying = true;
  160.    }
  161.    function stopEnterFrame()
  162.    {
  163.       if(this._fps == undefined)
  164.       {
  165.          _global.MovieClip.removeListener(this);
  166.       }
  167.       else
  168.       {
  169.          clearInterval(this._intervalID);
  170.       }
  171.       this.isPlaying = false;
  172.    }
  173.    function start()
  174.    {
  175.       this.rewind();
  176.       this.startEnterFrame();
  177.       this.broadcastMessage("onMotionStarted",this);
  178.    }
  179.    function stop()
  180.    {
  181.       this.stopEnterFrame();
  182.       this.broadcastMessage("onMotionStopped",this);
  183.    }
  184.    function resume()
  185.    {
  186.       this.fixTime();
  187.       this.startEnterFrame();
  188.       this.broadcastMessage("onMotionResumed",this);
  189.    }
  190.    function rewind(t)
  191.    {
  192.       this._time = t != undefined ? t : 0;
  193.       this.fixTime();
  194.       this.update();
  195.    }
  196.    function fforward()
  197.    {
  198.       this.time = this._duration;
  199.       this.fixTime();
  200.    }
  201.    function nextFrame()
  202.    {
  203.       if(this.useSeconds)
  204.       {
  205.          this.time = (getTimer() - this._startTime) / 1000;
  206.       }
  207.       else
  208.       {
  209.          this.time = this._time + 1;
  210.       }
  211.    }
  212.    function onEnterFrame()
  213.    {
  214.       this.nextFrame();
  215.    }
  216.    function prevFrame()
  217.    {
  218.       if(!this.useSeconds)
  219.       {
  220.          this.time = this._time - 1;
  221.       }
  222.    }
  223.    function toString()
  224.    {
  225.       return "[Tween]";
  226.    }
  227.    function fixTime()
  228.    {
  229.       if(this.useSeconds)
  230.       {
  231.          this._startTime = getTimer() - this._time * 1000;
  232.       }
  233.    }
  234.    function update()
  235.    {
  236.       this.position = this.getPosition(this._time);
  237.    }
  238.    function func(t, b, c, d)
  239.    {
  240.       return c * t / d + b;
  241.    }
  242. }
  243.