【百行代码说游戏】ActionScript3.0中TimerEvent与rotation的应用【个性时针】
1.新建一个ActionScript3.0的FLV文件
2.插入-->新建元件Hour,如图
点取相关关联
用工具栏画一个时针图形,尽量画在中心注册点位置
3.同理新建Minute元件
画一个分针图形
4.同理建一个Second元件
画一个秒针的图形
5.同理建一个Biaopan元件
任意画一个自己个性的表盘的图形
6.按F9添加ActionScript3.0 脚本代码
var now:Date = new Date(); //获取本地时间 var now_hour=now.getHours(); //获取时值,24小时制 var now_minute=now.getMinutes();//获取分值 var now_second=now.getSeconds(); //获取秒值 var angle_second = now_second/60/*360;//将值转换成表盘的刻度 var angle_minute= now_minute/60*360; var angle_hour = (now_hour+now_minute/60)/12*360; var biaopan:Biaopan = new Biaopan();//新建一个之前画的Biaopan元件对象 var hour: Hour= new Hour(); //新建一个之前画的Hour元件对象 var minute:Minute= new Minute(); //新建一个之前画的Minute元件对象 var second:Second = new Second(); //新建一个之前画的Second元件对象 addChild(biaopan); addChild(hour); addChild(minute); addChild(second); biaopan.x=stage.stageWidth/2; biaopan.y=stage.stageHeight/2; hour.x=stage.stageWidth/2; hour.y=stage.stageHeight/2; minute.x=stage.stageWidth/2; minute.y=stage.stageHeight/2; second.x=stage.stageWidth/2; second.y=stage.stageHeight/2; hour.rotation = angle_hour; //初始指针的位置 minute.rotation = angle_minute; second.rotation = angle_second; var timer:Timer ; timer = new Timer(1000); //每秒执行一次函数timeHandler timer.addEventListener(TimerEvent.TIMER, timeHandler); timer.start(); function timeHandler(e:Event):void { second.rotation += 6; //秒针每秒 转 6度 minute.rotation += 6/60; //分针针每秒 转 6/60度 hour.rotation += 6/3600; //时针每秒 转 6/3600度 }
7.作品效果
8.作品展示地址:http://up.qqhello.com/qzone/22c991982d981e4cebc857715921f479.swf
方法二:每次刷新本地时间,直接将时间更新图形最新位置。
var biaopan:Biaopan = new Biaopan();//新建一个之前画的Biaopan元件对象 var hour: Hour= new Hour(); //新建一个之前画的Hour元件对象 var minute:Minute= new Minute(); //新建一个之前画的Minute元件对象 var second:Second = new Second(); //新建一个之前画的Second元件对象 var timer:Timer ; timer = new Timer(1000); //每秒执行一次函数timeHandler timer.addEventListener(TimerEvent.TIMER, timeHandler); timer.start(); function timeHandler(e:Event):void { var now:Date = new Date(); //获取本地时间 var now_hour=now.getHours(); //获取时值,24小时制 var now_minute=now.getMinutes();//获取分值 var now_second=now.getSeconds(); //获取秒值 var angle_second = now_second/60*360;//将值转换成表盘的刻度 var angle_minute= now_minute/60*360; var angle_hour = (now_hour+now_minute/60)/12*360; biaopan.x=stage.stageWidth/2; biaopan.y=stage.stageHeight/2; hour.x=stage.stageWidth/2; hour.y=stage.stageHeight/2; minute.x=stage.stageWidth/2; minute.y=stage.stageHeight/2; second.x=stage.stageWidth/2; second.y=stage.stageHeight/2; hour.rotation = angle_hour; //初始指针的位置 minute.rotation = angle_minute; second.rotation = angle_second; addChild(biaopan); addChild(hour); addChild(minute); addChild(second) }