定时器结束的时候会发送timerComplete事件,响应这个事件,可令定时器反复运行。增加定时器的重复运行代码后,本例动画的代码准备就万事俱备了。把代码综合起来,完成本例。
var linetimer:Timer = new Timer(50, 150);
linetimer.addEventListener("timer", linetimerHandler);
linetimer.addEventListener("timerComplete", animComplete);
//三个运动点
var stoppoint:Array=new Array(3);
stoppoint[0]=new Point;
stoppoint[0].x=80;
stoppoint[0].y=120;
stoppoint[1]=new Point(380,310);
stoppoint[2]=new Point(450,70);
//在动画开始的时候把对象移动到stoppoint[0]
LineRunner.x=stoppoint[0].x-LineRunner.width/2;
LineRunner.y=stoppoint[0].y-LineRunner.height/2;
//打开定时器
linetimer.start();
//定时器触发响应函数
function linetimerHandler(event:TimerEvent):void {
//动画代码编写入口
var i:int=event.target.currentCount;
if(i<=50)
{
//操作从stoppoint[0]到stoppoint[1]
LineRunner.x+=((stoppoint[1].x-stoppoint[0].x)/50);
LineRunner.y+=((stoppoint[1].y-stoppoint[0].y)/50);
}
else if(i<=100 && i>50)
{
//操作从stoppoint[1]到stoppoint[2]
LineRunner.x+=((stoppoint[2].x-stoppoint[1].x)/50);
LineRunner.y+=((stoppoint[2].y-stoppoint[1].y)/50);
}
else if(i<=150 && i>100)
{
//操作从stoppoint[2]到stoppoint[0]
LineRunner.x+=((stoppoint[0].x-stoppoint[2].x)/50);
LineRunner.y+=((stoppoint[0].y-stoppoint[2].y)/50);
}
}
//定时器结束后,重设定时器并反复运行。
function animComplete(event:TimerEvent):void {
event.target.reset();
event.target..start();
}
以上代码已经非常清楚,这里就不再赘述。运行,可以观察到:对象根据代码的预期,在三个点之间做逆时针运动。其动画轨迹清参考图:

27

被折叠的 条评论
为什么被折叠?



