Advertisement
Guest User

Flashpunk PINGPONG tween

a guest
Feb 29th, 2012
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.flashpunk
  2. {
  3.     /**
  4.      * Base class for all Tween objects, can be added to any Core-extended classes.
  5.      */
  6.     public class Tween
  7.     {
  8.         /**
  9.          * Persistent Tween type, will stop when it finishes.
  10.          */
  11.         public static const PERSIST:uint = 0;
  12.        
  13.         /**
  14.          * Looping Tween type, will restart immediately when it finishes.
  15.          */
  16.         public static const LOOPING:uint = 1;
  17.        
  18.         /**
  19.          * Oneshot Tween type, will stop and remove itself from its core container when it finishes.
  20.          */
  21.         public static const ONESHOT:uint = 2;
  22.        
  23.         /**
  24.          * Ping Pong Tween type, A looping tween that will reverse itself when it finishes.
  25.          */
  26.         public static const PINGPONG:uint = 3;
  27.        
  28.         /**
  29.          * If the tween should update.
  30.          */
  31.         public var active:Boolean;
  32.        
  33.         /**
  34.          * The rate of execution of the tween.
  35.          */
  36.         public var rate:Number = 1;
  37.        
  38.         /**
  39.          * Tween completion callback.
  40.          */
  41.         public var complete:Function;
  42.        
  43.         /**
  44.          * Constructor. Specify basic information about the Tween.
  45.          * @param   duration        Duration of the tween (in seconds or frames).
  46.          * @param   type            Tween type, one of Tween.PERSIST (default), Tween.LOOPING, or Tween.ONESHOT.
  47.          * @param   complete        Optional callback for when the Tween completes.
  48.          * @param   ease            Optional easer function to apply to the Tweened value.
  49.          */
  50.         public function Tween(duration:Number, type:uint = 0, complete:Function = null, ease:Function = null)
  51.         {
  52.             _target = duration;
  53.             _type = type;
  54.             this.complete = complete;
  55.             _ease = ease;
  56.         }
  57.        
  58.         /**
  59.          * Updates the Tween, called by World.
  60.          */
  61.         public function update():void
  62.         {
  63.             if (_type == PINGPONG && _ponging)
  64.             {
  65.                 _time -= FP.timeInFrames ? 1 : FP.elapsed * rate;
  66.                 _t = _time / _target;
  67.                 if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
  68.                 if (_t <= 0)
  69.                 {
  70.                     _t = 0;
  71.                     _finish = true;
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 _time += FP.timeInFrames ? 1 : FP.elapsed * rate;
  77.                 _t = _time / _target;
  78.                 if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
  79.                 if (_time >= _target)
  80.                 {
  81.                     _t = 1;
  82.                     _finish = true;
  83.                 }
  84.             }
  85.         }
  86.        
  87.         /**
  88.          * Starts the Tween, or restarts it if it's currently running.
  89.          */
  90.         public function start():void
  91.         {
  92.             if(!_ponging) _time = 0;
  93.             if (_target == 0)
  94.             {
  95.                 active = false;
  96.                 return;
  97.             }
  98.             active = true;
  99.         }
  100.        
  101.         /**
  102.          * Immediately stops the Tween and removes it from its Tweener without calling the complete callback.
  103.          */
  104.         public function cancel():void
  105.         {
  106.             active = false;
  107.             if (_parent) _parent.removeTween(this);
  108.         }
  109.        
  110.         /** @private Called when the Tween completes. */
  111.         internal function finish():void
  112.         {
  113.             switch (_type)
  114.             {
  115.                 case PERSIST:
  116.                     _time = _target;
  117.                     active = false;
  118.                     break;
  119.                 case LOOPING:
  120.                     _time %= _target;
  121.                     _t = _time / _target;
  122.                     if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
  123.                     start();
  124.                     break;
  125.                 case ONESHOT:
  126.                     _time = _target;
  127.                     active = false;
  128.                     _parent.removeTween(this);
  129.                     break;
  130.                 case PINGPONG:
  131.                     _time = _target;
  132.                     if (_ease != null && _t > 0 && _t < -1) _t = _ease(_t);
  133.                     _ponging = !_ponging;
  134.                     start();
  135.                     break;
  136.             }
  137.             _finish = false;
  138.             if (complete != null) complete();
  139.         }
  140.        
  141.         /**
  142.          * True if the tween is ponging.
  143.          */
  144.         public function get ponging():Boolean { return _ponging;}
  145.        
  146.         /**
  147.          * The completion percentage of the Tween.
  148.          */
  149.         public function get percent():Number { return _time / _target; }
  150.         public function set percent(value:Number):void { _time = _target * value; }
  151.        
  152.         /**
  153.          * The current time scale of the Tween (after easer has been applied).
  154.          */
  155.         public function get scale():Number { return _t; }
  156.        
  157.         // Tween information.
  158.         /** @private */ private var _type:uint;
  159.         /** @private */ private var _ponging:Boolean = false;
  160.         /** @private */ protected var _ease:Function;
  161.         /** @private */ protected var _t:Number = 0;
  162.        
  163.         // Timing information.
  164.         /** @private */ protected var _time:Number;
  165.         /** @private */ protected var _target:Number;
  166.        
  167.         // List information.
  168.         /** @private */ internal var _finish:Boolean;
  169.         /** @private */ internal var _parent:Tweener;
  170.         /** @private */ internal var _prev:Tween;
  171.         /** @private */ internal var _next:Tween;
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement