8.5.5 重复运动

    定时器结束的时候会发送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(380310);

stoppoint[2]=new Point(45070);

//在动画开始的时候把对象移动到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();

}

以上代码已经非常清楚,这里就不再赘述。运行,可以观察到:对象根据代码的预期,在三个点之间做逆时针运动。其动画轨迹清参考图

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值