主要用到绘图和日期Date类
1、新建一fla文件Clock.fla,舞台宽高设置为300x300
2、新建一as文件Clock.as,并与Clock.fla关联
Clock.as:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.text.TextFormat;
import flash.display.Shape;
public class Clock extends Sprite
{
//时针
private var hoursHand:Shape;
//分针
private var minutesHand:Shape;
//秒针
private var secondsHand:Shape;
//表盘装饰物
private var clockFaceOrnament:Shape;
public function Clock()
{
init();
creatFaceHands();
}
private function init():void
{
//表盘
var clockFace:Shape = createCircle(4,0x999999,0xcccccc,120);
clockFace.x = 150;
clockFace.y = 150;
addChild(clockFace);
//表盘装饰物
clockFaceOrnament = new Shape();
clockFaceOrnament.x = 120;
clockFaceOrnament.y = 120;
addChild(clockFaceOrnament);
clockFaceOrnament.graphics.lineStyle(6,0x000000);
clockFaceOrnament.graphics.moveTo(0,0);
clockFaceOrnament.graphics.curveTo(10,-7.5,20,0);
clockFaceOrnament.graphics.moveTo(40,0);
clockFaceOrnament.graphics.curveTo(50,-7.5,60,0);
clockFaceOrnament.graphics.moveTo(0,50);
clockFaceOrnament.graphics.curveTo(30,70,60,50);
}
/**创建表盘时针,分针,秒针**/
private function creatFaceHands()
{
var sFontFormat:TextFormat = new TextFormat("Tahoma",12,0xff0000,true);
var bFontFormat:TextFormat = new TextFormat("Tahoma",16,0xff0000,true);
//刻度
for (var j:int = 1;j<=60;j++)
{
if(j%5==0)
{
//大刻度
var bKeDuS:Shape = createCircle(0,0x0,0xff0000,4);
bKeDuS.x = Math.cos(Math.PI *2/60*j)*112+150;
bKeDuS.y = Math.sin(Math.PI *2/60*j)*112+150;
addChild(bKeDuS);
}
else
{
//小刻度
var sKeDuS:Shape = createCircle(0,0x0,0xff0000,2);
sKeDuS.x = Math.cos(Math.PI *2/60*j)*112+150;
sKeDuS.y = Math.sin(Math.PI *2/60*j)*112+150;
addChild(sKeDuS);
}
}
//数字
for (var i:int=1; i<13; i++)
{
var numTxt:TextField = new TextField();
numTxt.selectable = false;
numTxt.defaultTextFormat = sFontFormat;
numTxt.text = i+"";
numTxt.x = Math.cos(2*Math.PI/12 * i-Math.PI/2)*100+145;
numTxt.y = Math.sin(2*Math.PI/12 * i-Math.PI/2)*100+142;
addChild(numTxt);
}
hoursHand = new Shape();
hoursHand.x = 150;
hoursHand.y = 150;
addChild(hoursHand);
hoursHand.graphics.lineStyle(8,0xffff00);
hoursHand.graphics.moveTo(0,0);
hoursHand.graphics.lineTo(0,-50);
minutesHand = new Shape();
minutesHand.x = 150;
minutesHand.y = 150;
addChild(minutesHand);
minutesHand.graphics.lineStyle(5,0x00ff00);
minutesHand.graphics.moveTo(0,0);
minutesHand.graphics.lineTo(0,-65);
//针座
var clockNeedle:Shape = createCircle(0,0x0,0x0,5);
addChild(clockNeedle);
clockNeedle.x = 150;
clockNeedle.y =150;
secondsHand = new Shape();
secondsHand.x = 150;
secondsHand.y = 150;
addChild(secondsHand);
secondsHand.graphics.lineStyle(3,0x000099);
secondsHand.graphics.moveTo(0,15);
secondsHand.graphics.lineTo(0,-80);
initClock();
}
/**初始化时针,分针,秒针角度**/
private function initClock()
{
var date:Date = new Date();
hoursHand.rotation = (date.hours)*30 + (date.minutes)*0.5+(date.seconds)*(1/120);
minutesHand.rotation = (date.minutes)*6 +(date.seconds)*0.1;
secondsHand.rotation = (date.seconds)*6;
addEventListener(Event.ENTER_FRAME,updateClockRotation);
}
/**时时更新时针,分针,秒针角度**/
private function updateClockRotation(e:Event):void
{
var cor:ColorTransform = new ColorTransform();
cor.color = 0xffffff * Math.random();
clockFaceOrnament.transform.colorTransform = cor;
var date:Date = new Date();
hoursHand.rotation = date.getHours() * 30 + date.getMinutes()*0.5+(date.seconds)*(1/120);
minutesHand.rotation = date.getMinutes() * 6 + date.getSeconds()*0.1;
secondsHand.rotation = date.getSeconds() * 6;
}
//绘制圆的公共方法
private function createCircle(lineSize:int,lineColor:uint,fillColor:uint,radius:int):Shape
{
var tempShape:Shape = new Shape();
tempShape.graphics.clear();
if(lineSize)
{
tempShape.graphics.lineStyle(lineSize,lineColor);
}
tempShape.graphics.beginFill(fillColor,1);
tempShape.graphics.drawCircle(0,0,radius);
tempShape.graphics.endFill();
return tempShape;
}
}
}
Ctrl+Enter导出效果: