デジタル/アナログ時計(flash on 2009-8-18)
/**
* Copyright krogue ( http://wonderfl.net/user/krogue )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3xFY
*/
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.utils.Timer;
import flash.events.TimerEvent;
[SWF(width="640", height="480", backgroundColor="0xFFFFFF", frameRate="60")]
public class Clock extends Sprite {
private var timer:Timer;
// for digital
private var textField:TextField;
// for analog
private var hourHand:Shape;
private var minHand:Shape;
private var secHand:Shape;
public function Clock() {
addDisplayObjectForDigital();
addDisplayObjectForAnalog();
showCurrentTime();
timer = new Timer(100, 0);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function addDisplayObjectForDigital():void {
var textFormat:TextFormat = new TextFormat();
textFormat.size = 48;
textFormat.align = TextFormatAlign.CENTER;
textField = new TextField();
textField.defaultTextFormat = textFormat;
textField.x = 0;
textField.y = 0;
textField.width = 300;
textField.autoSize = TextFieldAutoSize.NONE;
addChild(textField);
}
private function addDisplayObjectForAnalog():void {
const cx:uint = 150;
const cy:uint = 200;
hourHand = new Shape();
hourHand.graphics.beginFill(0xFF0000);
hourHand.graphics.drawRect(0, 0, 1, 50);
hourHand.graphics.endFill();
hourHand.x = cx;
hourHand.y = cy;
addChild(hourHand);
minHand = new Shape();
minHand.graphics.beginFill(0x00FF00);
minHand.graphics.drawRect(0, 0, 1, 80);
minHand.graphics.endFill();
minHand.x = cx;
minHand.y = cy;
addChild(minHand);
secHand = new Shape();
secHand = new Shape();
secHand.graphics.beginFill(0x0000FF);
secHand.graphics.drawRect(0, 0, 1, 100);
secHand.graphics.endFill();
secHand.x = cx;
secHand.y = cy;
addChild(secHand);
const r:uint = 100;
for (var i:int = 1; i <= 12; i++) {
var textFormat:TextFormat = new TextFormat();
textFormat.size = 20;
textFormat.align = TextFormatAlign.CENTER;
var textField:TextField = new TextField();
textField.defaultTextFormat = textFormat;
textField.width = 25;
textField.height = 25;
textField.x = cx - textField.width / 2 +
Math.cos((-90 + i * 360 / 12) / 180 * Math.PI) * r;
textField.y = cy - textField.height / 2 +
Math.sin((-90 + i * 360 / 12) / 180 * Math.PI) * r;
textField.autoSize = TextFieldAutoSize.NONE;
textField.text = i.toString();
addChild(textField);
}
}
private function timerHandler(event:TimerEvent):void {
showCurrentTime();
}
private function showCurrentTime():void {
var currentTime:Date = new Date();
// for digital
var time:Array = new Array();
if (currentTime.hours < 10) {
time.push("0" + currentTime.hours);
} else {
time.push(currentTime.hours.toString());
}
if (currentTime.minutes < 10) {
time.push("0" + currentTime.minutes);
} else {
time.push(currentTime.minutes.toString());
}
if (currentTime.seconds < 10) {
time.push("0" + currentTime.seconds);
} else {
time.push(currentTime.seconds.toString());
}
textField.text = time.join(":");
// for analog
hourHand.rotation = 180 + (currentTime.hours * 360 / 12) +
(currentTime.minutes * 360 / 12 / 60) +
(currentTime.seconds * 360 / 12 / 60 / 60) +
(currentTime.milliseconds * 360 / 12 / 60 / 1000);
minHand.rotation = 180 + (currentTime.minutes * 360 / 60) +
(currentTime.seconds * 360 / 60 / 60) +
(currentTime.milliseconds * 360 / 60 / 60 / 1000);
secHand.rotation = 180 + (currentTime.seconds * 360 / 60) +
(currentTime.milliseconds * 360 / 60 / 1000);
}
}
}