home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Acao / kung_fu.swf / scripts / __Packages / ds / transitions / Tween.as next >
Encoding:
Text File  |  2006-06-13  |  5.6 KB  |  265 lines

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