package {
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class Main extends Sprite
{
private var r:Number = 155;//钟面半径
private var cx:Number = 175;//钟面圆心x
private var cy:Number = 175;//钟面圆心y
private var pr:Number = 142;//整点半径
private var sl:Number = 140;//秒针长度
private var ml:Number = 100;//分针长度
private var hl:Number = 65;//时针长度
private var clock:Sprite = new Sprite();
private var finger:Sprite = new Sprite();
private var timer:Timer = new Timer(0);
public function Main()
{
clock.addChild(finger);
this.addChild(clock);
initClock();
}
public function initClock():void
{
clock.graphics.lineStyle(5,0x007500);
clock.graphics.drawCircle(cx,cy,r+1);
clock.graphics.drawCircle(cx,cy,r);
clock.graphics.beginFill(0x00ff00);
clock.graphics.drawCircle(cx,cy,8);
// add logo
addLogo();
initClockPoint();
timer.addEventListener(TimerEvent.TIMER, initClockFinger);
timer.start();
}
//画整点
public function initClockPoint():void
{
for (var i:int=0; i<60; i++)
{
if(i%5==0){
clock.graphics.lineStyle(5,0x000000);
var h:int = i/5;
var px:Number = Math.sin(Math.PI*h/6)*pr;
var py:Number = Math.cos(Math.PI*h/6)*pr;
clock.graphics.beginFill(0x000000);
clock.graphics.drawCircle(cx+px, cy-py, 2);
var nx:Number = Math.sin(Math.PI*h/6)*(pr-20);
var ny:Number = Math.cos(Math.PI*h/6)*(pr-20);
if(i==0){
addPointNum(String(12),cx+nx, cy-ny);
}else{
addPointNum(String(h),cx+nx, cy-ny);
}
}else{
clock.graphics.lineStyle(3,0x3C3C3C);
var spx:Number = cx + Math.sin(Math.PI*i/30)*pr;
var spy:Number = cy - Math.cos(Math.PI*i/30)*pr;
clock.graphics.drawCircle(spx, spy, 1);
}
}
}
private function addPointNum(txt:String,x:Number,y:Number){
var format:TextFormat = new TextFormat();
format.size = 32;
format.color=0x000000;
var num_txt:TextField = new TextField();
num_txt.selectable = false;
num_txt.htmlText = txt;
num_txt.x =x-50;
num_txt.y =y-20;
num_txt.setTextFormat(format);
num_txt.autoSize = TextFieldAutoSize.CENTER;
clock.addChild(num_txt);
}
private function addLogo(){
var format:TextFormat = new TextFormat();
format.size = 32;
format.color=0x000000;
format.bold = true;
var num_txt:TextField = new TextField();
num_txt.selectable = false;
num_txt.htmlText ="CYS";
num_txt.x =cx-50;
num_txt.y =cy+20;
num_txt.setTextFormat(format);
num_txt.autoSize = TextFieldAutoSize.CENTER;
clock.addChild(num_txt);
}
//初始化指针
public function initClockFinger(event:TimerEvent):void
{
finger.graphics.clear();
var time:Date = new Date();
var sp:Point = new Point();//秒针
sp.x = cx + Math.sin(Math.PI*time.seconds/30)*sl;
sp.y = cy - Math.cos(Math.PI*time.seconds/30)*sl;
var mp:Point = new Point();//分针
mp.x = cx + Math.sin(Math.PI*time.minutes/30)*ml;
mp.y = cy - Math.cos(Math.PI*time.minutes/30)*ml;
var hp:Point = new Point();//时针
var hh:Number = time.hours;
if (time.hours>=12)
hh = hh - 12;
hp.x = cx + Math.sin(Math.PI*(hh+time.minutes/60)/6)*hl;
hp.y = cy - Math.cos(Math.PI*(hh+time.minutes/60)/6)*hl;
//画指针
finger.graphics.moveTo(cx, cy);
finger.graphics.lineStyle(10,0x3C3C3C);
finger.graphics.lineTo(hp.x, hp.y);
finger.graphics.moveTo(cx, cy);
finger.graphics.lineStyle(8,0x00FFFF);
finger.graphics.lineTo(mp.x, mp.y);
finger.graphics.moveTo(cx, cy);
finger.graphics.lineStyle(5,0xCD853F);
finger.graphics.lineTo(sp.x, sp.y); }
}
}