Flash 模拟时钟

主要用到绘图和日期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导出效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西溪漫步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值