模拟时钟

package
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.Timer;

	public class clock extends Sprite
	{
		public var ticker: Timer;
		
		public static const millisecondsPerMinute:int = 1000 * 60;
		public static const millisecondsPerHour:int = 1000 * 60 * 60;
		public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;
		
		public var radius:uint = 100;
		public var centerX:int = 100;
		public var centerY:int = 100;
		public var hourHand:Shape;
		public var minuteHand:Shape;
		public var secondHand:Shape;
		public var currentTime:Date;
		
		
		public function clock()
		{TimerEvent
						
			init();
			draw();
			
			//创建一个Timer每隔1s执行一次
			ticker = new Timer(1000);
			
			//绑定事件
			ticker.addEventListener(TimerEvent.TIMER,onTick);
		
			ticker.start();
		}
		
		public function onTick(event:TimerEvent):void
		{
			draw();
		}
			
		public function init():void 
		{
			drawBorder();
			drawLabels();
			createHands();
		}
		
		public function drawBorder():void
		{
			graphics.lineStyle(1, 0x999999);
			graphics.beginFill(0xEEEEFF);
			graphics.drawCircle(centerX, centerY, radius);
			graphics.endFill();
//			设置表盘读书
			var myText:Array=["1","2","3","4","5","6","7","8","9","10","11","12"];
			var myTextBox0:TextField=new TextField();
			var myTextBox1:TextField=new TextField();
			var myTextBox2:TextField=new TextField();
			var myTextBox3:TextField=new TextField();
			var myTextBox4:TextField=new TextField();
			var myTextBox5:TextField=new TextField();
			var myTextBox6:TextField=new TextField();
			var myTextBox7:TextField=new TextField();
			var myTextBox8:TextField=new TextField();
			var myTextBox9:TextField=new TextField();
			var myTextBox10:TextField=new TextField();
			var myTextBox11:TextField=new TextField();
			myTextBox0.height=200;
			myTextBox0.width=200;
			myTextBox1.height=200;
			myTextBox1.width=200;
			myTextBox2.height=200;
			myTextBox2.width=200;
			myTextBox3.height=200;
			myTextBox3.width=200;
			myTextBox4.height=200;
			myTextBox4.width=200;
			myTextBox5.height=200;
			myTextBox5.width=200;
			myTextBox6.height=200;
			myTextBox6.width=200;
			myTextBox7.height=200;
			myTextBox7.width=200;
			myTextBox8.height=200;
			myTextBox8.width=200;
			myTextBox9.height=200;
			myTextBox9.width=200;
			myTextBox10.height=200;
			myTextBox10.width=200;
			myTextBox11.height=200;
			myTextBox11.width=200;
			this.addChild(myTextBox0);
			this.addChild(myTextBox1);
			this.addChild(myTextBox2);
			this.addChild(myTextBox3);
			this.addChild(myTextBox4);
			this.addChild(myTextBox5);
			this.addChild(myTextBox6);
			this.addChild(myTextBox7);
			this.addChild(myTextBox8);
			this.addChild(myTextBox9);
			this.addChild(myTextBox10);
			this.addChild(myTextBox11);
			for(var i:uint=0;i<myText.length;i++)
			{
				switch(i){
				         case  0:
							 var format0:TextFormat = new TextFormat();
							 format0.leftMargin=135;
							 format0.size=16;
							 myTextBox0.y=25;
							 myTextBox0.appendText(myText[i]);
							 myTextBox0.setTextFormat(format0);
							 break;
						 case  1:
							 var format1:TextFormat = new TextFormat();
							 format1.leftMargin=160;							 
							 format1.size=16;
							 myTextBox1.y=50;
							
							 myTextBox1.appendText(myText[i]);
							 myTextBox1.setTextFormat(format1);
							 break;	 
						 case  2:
							 var format2:TextFormat = new TextFormat();
							 format2.leftMargin=170;							 
							 format2.size=16;
							 myTextBox2.y=90;
							
							 myTextBox2.appendText(myText[i]);
							 myTextBox2.setTextFormat(format2);
							 break;	
						 case  3:
							 var format3:TextFormat = new TextFormat();
							 format3.leftMargin=160;							 
							 format3.size=16;
							 myTextBox3.y=127;							 
							 myTextBox3.appendText(myText[i]);
							 myTextBox3.setTextFormat(format3);
							 break;	
						 case  4:
							 var format4:TextFormat = new TextFormat();
							 format4.leftMargin=135;							 
							 format4.size=16;
							 myTextBox4.y=153;						 
							 myTextBox4.appendText(myText[i]);
							 myTextBox4.setTextFormat(format4);
							 break;	
				
							trace(myText[i]);
						case  5:
								var format5:TextFormat = new TextFormat();
								format5.leftMargin=95;							 
								format5.size=16;
								myTextBox5.y=163;						 
								myTextBox5.appendText(myText[i]);
								myTextBox5.setTextFormat(format5);
								break;	
						case  6:
							var format6:TextFormat = new TextFormat();
							format6.leftMargin=58;							 
							format6.size=16;
							myTextBox6.y=153;							 
							myTextBox6.appendText(myText[i]);
							myTextBox6.setTextFormat(format6);
							break;		
							trace(myText[i]);
						case  7:
							var format7:TextFormat = new TextFormat();
							format7.leftMargin=30;							 
							format7.size=16;
							myTextBox7.y=127;							 
							myTextBox7.appendText(myText[i]);
							myTextBox7.setTextFormat(format7);
							break;	
						case  8:
							var format8:TextFormat = new TextFormat();
							format8.leftMargin=18;							 
							format8.size=16;
							myTextBox8.y=90;						 
							myTextBox8.appendText(myText[i]);
							myTextBox8.setTextFormat(format8);
							break;	
						case  9:
							var format9:TextFormat = new TextFormat();
							format9.leftMargin=30;							 
							format9.size=16;
							myTextBox9.y=50;						 
							myTextBox9.appendText(myText[i]);
							myTextBox9.setTextFormat(format9);
							break;	
						case  10:
							var format10:TextFormat = new TextFormat();
							format10.leftMargin=58;							 
							format10.size=16;
							myTextBox10.y=25;							 
							myTextBox10.appendText(myText[i]);
							myTextBox10.setTextFormat(format10);
							break;	
						case  11:
							var format11:TextFormat = new TextFormat();
							format11.leftMargin=90;							 
							myTextBox11.y=15;							 
							format11.size=16;
							myTextBox11.appendText(myText[i]);
							myTextBox11.setTextFormat(format11);
							break;					
				}
			}
		}

		public function drawLabels():void
		{
			for (var i:Number = 0; i < 12; i++)
			{
				var angleInRadians:Number = i * 30 * (Math.PI/180);
				var graduation:Shape = new Shape();
				graduation.graphics.lineStyle(3.0, 0x000000);
				graduation.graphics.moveTo(0,12);
				graduation.graphics.lineTo(0, 0);
				graduation.x = centerX + (0.98 * radius * Math.sin( angleInRadians ));
				graduation.y = centerY - (0.98 * radius * Math.cos( angleInRadians ));
				graduation.rotation = (i * 30);
				addChild(graduation);			
			}
		}
		
		
		public function createHands():void
		{
			var hourHandShape:Shape = new Shape();
			drawHand(hourHandShape, Math.round(radius * 0.5), 0x003366, 3.0);
			hourHand = Shape(addChild(hourHandShape));
			hourHand.x = centerX;
			hourHand.y = centerY;
			
			var minuteHandShape:Shape = new Shape();
			drawHand(minuteHandShape, Math.round(radius * 0.8), 0x000099, 2.0);
			minuteHand = Shape(addChild(minuteHandShape));
			minuteHand.x = centerX;
			minuteHand.y = centerY;
			
			var secondHandShape:Shape = new Shape();
			drawHand(secondHandShape, Math.round(radius * 0.9), 0xCC0033, 0.5);
			secondHand = Shape(addChild(secondHandShape));
			secondHand.x = centerX;
			secondHand.y = centerY;
		}
		
		public function drawHand(hand:Shape, distance:uint, color:uint, thickness:Number):void
		{
			hand.graphics.lineStyle(thickness, color);
			hand.graphics.moveTo(0, distance);
			hand.graphics.lineTo(0, 0);
		}
		
		public function draw():void
		{
			currentTime = new Date();
			showTime(currentTime);
		}
		
		public function showTime(time:Date):void 
		{
			var seconds:uint = time.getSeconds();
			var minutes:uint = time.getMinutes();
			var hours:uint = time.getHours();
			secondHand.rotation = 180 + (seconds * 6);
			minuteHand.rotation = 180 + (minutes * 6);
		
			hourHand.rotation = 180 + (hours * 30) + (minutes * 0.5);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值