package { import flash.display.Sprite; import flash.events.Event; [SWF(width="100", height="100", backgroundColor="0xffffff", frameRate="30")] public class Clock extends Sprite { public var radius:int = 50; public var ticks:int = 0; public var face:Sprite = new Sprite; public function Clock():void { // Increment "ticks" to correct value var now:Date = new Date(); ticks += now.seconds * 30; ticks += now.minutes * 30 * 60; ticks += now.hours * 30 * 60 * 60; // Draw background graphics.beginFill(0xcccccc); graphics.drawCircle(50,50,radius); graphics.endFill(); // Draw notches - 60 of 'em, every 6 degrees var notchSize:int; for(var i:int = 0; i < 360; i += 6) { // Convert from degrees to radians, multiply by radius, then offset by radius (otherwise drawn around 0,0) graphics.moveTo((Math.cos(toRadians(i)) * radius) + radius, (Math.sin(toRadians(i)) * radius) + radius); notchSize = (i % 5 == 0) ? 4 : 2; graphics.lineStyle(notchSize, 0x000000, 0.8); graphics.lineTo(Math.cos(toRadians(i)) * (radius - notchSize) + radius, (Math.sin(toRadians(i)) * (radius - notchSize)) + radius); } // Add "update" event listener addEventListener(Event.ENTER_FRAME, update); // Add the clock "face" which we'll draw hands on addChild(face); } public function update(e:Event):void { ticks++; face.graphics.clear(); // Draw seconds face.graphics.lineStyle(2, 0x00ffff, 0.9) face.graphics.moveTo(50,50); face.graphics.lineTo((Math.cos(toRadians(ticks / 5)) * radius) + radius, (Math.sin(toRadians(ticks / 5)) * radius) + radius); // Draw minutes face.graphics.lineStyle(4, 0xff00ff, 0.8) face.graphics.moveTo(50,50); face.graphics.lineTo((Math.cos(toRadians(ticks / 300)) * radius) + radius, (Math.sin(toRadians(ticks / 300)) * radius) + radius); // Draw hours face.graphics.lineStyle(6, 0xffff00, 0.7) face.graphics.moveTo(50,50); face.graphics.lineTo((Math.cos(toRadians(ticks / 3600)) * radius) + radius, (Math.sin(toRadians(ticks / 3600)) * radius) + radius); } } } function toRadians(degrees:Number):Number { return (degrees * Math.PI / 180) - (90 * Math.PI / 180); } Clock