【AS3代码】动态时钟
//绘制刻度类
package com
{
import flash.display.Shape;
public class Box extends Shape
{
public function Box()
{
init();
}
private function init():void
{
this.graphics.beginFill(0x00ff00);
this.graphics.drawRect(-3, -85, 6, 25); //矩形条宽6
this.graphics.endFill();
}
}
}
{
import flash.display.Shape;
public class Box extends Shape
{
public function Box()
{
init();
}
private function init():void
{
this.graphics.beginFill(0x00ff00);
this.graphics.drawRect(-3, -85, 6, 25); //矩形条宽6
this.graphics.endFill();
}
}
}
//绘制时钟的指针类
package com
{
import flash.display.Shape;
public class Xian extends Shape
{
//len长度,color颜色
public function Xian(len:uint, color:uint):void
{
init(len, color);
}
private function init(len:uint, color:uint):void
{
this.graphics.lineStyle(2,color);
this.graphics.moveTo(0,0); //线条的起点(原点)
this.graphics.lineTo(0,len * -1); //线条的终点(乘以负数,向上画;乘以正数,向下画;)
this.graphics.endFill();
}
}
}
{
import flash.display.Shape;
public class Xian extends Shape
{
//len长度,color颜色
public function Xian(len:uint, color:uint):void
{
init(len, color);
}
private function init(len:uint, color:uint):void
{
this.graphics.lineStyle(2,color);
this.graphics.moveTo(0,0); //线条的起点(原点)
this.graphics.lineTo(0,len * -1); //线条的终点(乘以负数,向上画;乘以正数,向下画;)
this.graphics.endFill();
}
}
}
文档类
package
{
import com.Box;
import com.Xian;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
//绘制三条指针
private var shizhen:Xian = new Xian(25, 0x000000); //时针
private var fenzhen:Xian = new Xian(40, 0xff00ff); //分针
private var miaozhen:Xian = new Xian(55, 0xff0000); //秒针
private var shijian:Date;
public function Main():void
{
init();
}
private function init():void
{
var obj:Sprite = new Sprite();
obj.x = 250;
obj.y = 180;
this.addChild(obj);
//绘制表盘
for(var i:uint = 0; i < 12; i++)
{
var biaoshi:Box = new Box();
biaoshi.rotation += i * 30; //每生成一个,就在之前的角度上旋转30度
obj.addChild(biaoshi);
}
obj.addChild(shizhen);
obj.addChild(fenzhen);
obj.addChild(miaozhen);
addEventListener(Event.ENTER_FRAME, dong);
}
//指针转动函数
private function dong(evt:Event):void
{
shijian = new Date();
//秒针的旋转角度算法:转一周360度,一分钟60秒,所以每一秒也就是旋转6度。
miaozhen.rotation = shijian.seconds * 6;
//分针的旋转角度算法:和秒针同理,每一分钟也是旋转6度,再加上每一条针也要转动的角度
fenzhen.rotation = shijian.minutes *6 + shijian.seconds * 0.1;
//时针旋转角度算法:
shizhen.rotation = shijian.hours * 30 + shijian.minutes * 0.5 + shijian.seconds * 0.0083;
}
}
{
import com.Box;
import com.Xian;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
//绘制三条指针
private var shizhen:Xian = new Xian(25, 0x000000); //时针
private var fenzhen:Xian = new Xian(40, 0xff00ff); //分针
private var miaozhen:Xian = new Xian(55, 0xff0000); //秒针
private var shijian:Date;
public function Main():void
{
init();
}
private function init():void
{
var obj:Sprite = new Sprite();
obj.x = 250;
obj.y = 180;
this.addChild(obj);
//绘制表盘
for(var i:uint = 0; i < 12; i++)
{
var biaoshi:Box = new Box();
biaoshi.rotation += i * 30; //每生成一个,就在之前的角度上旋转30度
obj.addChild(biaoshi);
}
obj.addChild(shizhen);
obj.addChild(fenzhen);
obj.addChild(miaozhen);
addEventListener(Event.ENTER_FRAME, dong);
}
//指针转动函数
private function dong(evt:Event):void
{
shijian = new Date();
//秒针的旋转角度算法:转一周360度,一分钟60秒,所以每一秒也就是旋转6度。
miaozhen.rotation = shijian.seconds * 6;
//分针的旋转角度算法:和秒针同理,每一分钟也是旋转6度,再加上每一条针也要转动的角度
fenzhen.rotation = shijian.minutes *6 + shijian.seconds * 0.1;
//时针旋转角度算法:
shizhen.rotation = shijian.hours * 30 + shijian.minutes * 0.5 + shijian.seconds * 0.0083;
}
}
}