// This working file can be copied to any of the
// My Scripts folders and renamed effect.txt.
// This provides documentation on how to modify this
// file to create your own motion script.
// Feel free to use the other effect.txt files as
// inspiration.

//
// these variables must be defined
//

mcN = "letter";

// pause between characters in milliseconds
delay = 400;
 
// loop = 0 or 1
loop = 0;

// pause between loops in milliseconds
loopDelay = 0; 

// waitCharEnd = 0 or 1
// waitCharEnd == 0, all characters acted on at once
// waitCharEnd == 1, characters acted on in sequence
waitCharEnd = 0; 
 
// random = 0 or 1
random = 0;
 
// reverse = 0 or 1
// reverse == 0, left to right effect
// reverse == 1, right to left effect
reverse = 0; 

//
// these variables are specific to this effect
//

increments = 6;
alpha = 0;
xscale = 0;
yscale = 0;
tilt  = -45;
startAngle = 45;
finalAngle = -45;
a = 150;
b = 20;
pi = 3.1416;
startRadian = startAngle * pi / 180;
finalRadian = finalAngle * pi / 180;
xOffset     = a * Math.cos(finalRadian);
yOffset     = b * Math.sin(finalRadian);
xscaleInc   = Math.floor((100 - xscale) / increments);
yscaleInc   = Math.floor((100 - yscale) / increments);
alphaInc    = Math.floor((100 - alpha) / increments);
radianInc   = (finalRadian - startRadian) / increments;
tiltInc    = -tilt / increments;

//
// this must be present
//

aLetters = new Array();

//
// this must be present
//

for (i = 0; i< numChar; i++){
  aLetters[i+0] = i;
  var letter =  this[mcN +i];
  letter._visible = false;
  letter.init = letterInit;
  letter.doEffect = effect;
  letter.number = i;
}

//
// perform initializations here
//

function letterInit(){
  this._visible = true;
  this.step = 0;
  this.increments = this._parent.increments;
  this.xscaleInc = this._parent.xscaleInc;
  this.yscaleInc = this._parent.yscaleInc;
  this._xscale = this._parent.xscale;
  this._yscale = this._parent.yscale;
  this._alpha = this._parent.alpha;
  this.alphaInc = this._parent.alphaInc;
  this.a = this._parent.a;
  this.b = this._parent.b;
  this.xFinal = this._x;
  this.yFinal = this._y;
  this.xOffset = this._parent.xOffset;
  this.yOffset = this._parent.yOffset;
  this._x = this._x - this.xOffset;
  this._y = this._y + this.yOffset;
  this.radianInc = this._parent.radianInc;
  this.radian = this._parent.startRadian;
  this.tiltInc = this._parent.tiltInc;
  this._rotation = this._parent.tilt;
}

//
// define the effect
//

function effect(){
  this._xscale   += this.xscaleInc;
  this._yscale   += this.yscaleInc;
  this._alpha    += this.alphaInc;
  this._rotation += this.tiltInc;
  this.radian    += this.radianInc;

  xDist        = a * Math.cos(radian);
  yDist        = b * Math.sin(radian);

  this._x      = this.xFinal + xDist - this.xOffset;
  this._y      = this.yFinal - yDist + this.yOffset;

  this.step += 1;

  if (this.step >= this.increments){
    this._xscale = 100;
    this._yscale = 100;
    this._alpha = 100;
    this._x = this.xFinal;
    this._y = this.yFinal;
    this._rotation = 0;
    this.gotoAndStop("end");
  }
}

//
// this must be defined
//

function shuffle(){
  return Math.floor(Math.random() * 3) -1;
}

//
// this must be defined
//

if (random == 1){
  aLetters.sort(shuffle);
}

//
// this must be defined
//

if (reverse == 1){
  aLetters.reverse();
}
