package org.libspark.eseclock { import flash.display.DisplayObject; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; public class Eseclock extends Sprite { public function Eseclock(color1:uint, color2:uint) { _color1 = color1; _color2 = color2; addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); } private var _color1:uint; private var _color2:uint; private function addedToStageHandler(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); var sw:Number = stage.stageWidth; var sh:Number = stage.stageHeight; var clock1:ClockSprite = new ClockSprite(_color1, _color2, sw, sh); var clock2:ClockSprite = new ClockSprite(_color2, _color1, sw, sh); _clock1 = clock1; _clock2 = clock2; addChild(clock1); addChild(clock2); _prevDate = new Date(); addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private var _clock1:ClockSprite; private var _clock2:ClockSprite; private var _prevDate:Date; private var _contents:DisplayObject; protected function createContents(w:Number, h:Number):DisplayObject { var shape:Shape = new Shape(); shape.graphics.beginFill(Math.floor(Math.random() * 256)); shape.graphics.drawRect(0, 0, w, h); shape.graphics.endFill(); return shape; } private function enterFrameHandler(e:Event):void { var date:Date = new Date(); if (_prevDate.seconds != date.seconds) { if (date.seconds % 2 == 0) { _clock1.visible = false; _clock2.visible = true; } else { _clock1.visible = true; _clock2.visible = false; } if (date.seconds % 10 == 1) { if (_contents != null) { removeChild(_contents); _contents = null; } } if (date.seconds % 10 == 6) { _contents = createContents(stage.stageWidth, stage.stageHeight); addChild(_contents); } } _prevDate = date; _clock1.update(date.hours, date.minutes, date.seconds); _clock2.update(date.hours, date.minutes, date.seconds); } } } import flash.display.Shape; import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; internal class ClockSprite extends Sprite { public function ClockSprite(textColor:uint, backgroundColor:uint, w:Number, h:Number) { var shape:Shape = new Shape(); shape.graphics.beginFill(backgroundColor); shape.graphics.drawRect(0, 0, w, h); shape.graphics.endFill(); addChild(shape); _field = new TextField(); _field.defaultTextFormat = new TextFormat('Arial Black', 96, textColor, true); _field.autoSize = TextFieldAutoSize.LEFT; _field.text = '99 99 99'; _field.x = (w - _field.textWidth) / 2; _field.y = (h - _field.textHeight) / 2; addChild(_field); } private var _field:TextField; public function update(h:uint, m:uint, s:uint):void { _field.text = toDisplayString(h) + ' ' + toDisplayString(m) + ' ' + toDisplayString(s); } private function toDisplayString(n:uint):String { return ('0' + n.toString()).substr(-2, 2); } }