[AS3] CeaseOut

// CeaseOut.as
package Classes {
 import flash.display.MovieClip;
 import flash.events.Event;
 
 public class CeaseOut {
  private var mc:MovieClip;
  private var nX:Number;
  private var nY:Number;
  private var nS:Number;
  
  private var nCalcX:Number;
  private var nCalcY:Number;
  private var nDist:Number;
  
  public function CeaseOut(mc:MovieClip, nX:Number, nY:Number, nS:Number=10) {
   this.mc = mc;
   this.nX = nX;
   this.nY = nY;
   this.nS = nS;
   
   this.mc.addEventListener(Event.ENTER_FRAME, this.contRefresh);
  }
  
  public function cleanUp():void {
   this.mc.removeEventListener(Event.ENTER_FRAME, this.contRefresh);
  }
  
  private function contRefresh(e:Event):void {
   this.nCalcX = this.nX – this.mc.x;
   this.nCalcY = this.nY – this.mc.y;
   
   this.mc.x += this.nCalcX / this.nS;
   this.mc.y += this.nCalcY / this.nS;
   
   this.nDist = Math.sqrt( Math.pow(this.nCalcX, 2) + Math.pow(this.nCalcY, 2) );
   
   if (this.nDist < 0.1) {
    this.mc.removeEventListener(Event.ENTER_FRAME, this.contRefresh);
   }
  }
 }
}

// usage
import Classes.CeaseOut;
var myEaseOut:CeaseOut = new CeaseOut(mc, 100, 100);

~ by rotaercz on 2008-January-27.

Leave a Reply