package {
import flash.display.*;
import flash.text.*;
import flash.geom.Point;
import flash.events.*;
import flash.utils.Timer;
public class TextFly extends MovieClip {
// 定义动画的常量
static const spacing:Number = 50;//间隔
static const phrase:String = "MeetLunay";
static const numSteps:int = 50;
static const stepTime:int = 20;
static const totalRotation:Number = 360;//整体旋转
static const startScale:Number = 0.0;
static const endScale:Number = 2.0;//缩放结束
static const startLoc:Point = new Point(250,0);//起始位置
static const endLoc:Point = new Point(50,100);//结束位置
private var letterFormat:TextFormat = new TextFormat("Courier",36,0x000000,true,false,false,null,null,TextFormatAlign.CENTER);
// 跟踪动画的变量
private var letters:Array = new Array();
private var flySprite:Sprite;
private var animTimer:Timer;
public function TextFly() {
// 保存所有元素sprite
flySprite = new Sprite();
addChild(flySprite);
// 为每个字母创建文本字段,并放入sprite中
for(var i:int=0;i<phrase.length;i++) {
var letter:TextField = new TextField();
letter.defaultTextFormat = letterFormat;
letter.embedFonts = true;
letter.autoSize = TextFieldAutoSize.CENTER;
letter.text = phrase.substr(i,1);
letter.x = -letter.width/2;
letter.y = -letter.height/2;
var newSprite:Sprite = new Sprite();
newSprite.addChild(letter);
newSprite.x = startLoc.x;
newSprite.y = startLoc.y;
flySprite.addChild(newSprite);
letters.push(newSprite);
}
// 开始动画
animTimer = new Timer(stepTime,numSteps);
animTimer.addEventListener(TimerEvent.TIMER,animate);
animTimer.start();
}
public function animate(event:TimerEvent) {
// 动画的长度
var percentDone:Number = event.target.currentCount/event.target.repeatCount;
// 改变位置, 尺寸和旋转
for(var i:int=0;i<letters.length;i++) {
letters[i].x = startLoc.x*(1.0-percentDone) + (endLoc.x+spacing*i)*percentDone;
letters[i].y = startLoc.y*(1.0-percentDone) + endLoc.y*percentDone;
var scale:Number = startScale*(1-percentDone)+endScale*percentDone;
letters[i].scaleX = scale;
letters[i].scaleY = scale;
letters[i].rotation = totalRotation*(percentDone-1);
}
}
}
}