View Code
package mvc { public class Time { private var _hour:uint; private var _minute:uint; private var _second:uint; public function Time(hour:uint,minute:uint,second:uint){ this._hour=hour; this._minute=minute; this._second=second; } public function get hour():uint{ return _hour; } public function set hour(value:uint):void{ this._hour=value; } public function get minute():uint{ return _minute; } public function set minute(value:uint):void{ this._minute=value; } public function get second():uint{ return _second; } public function set second(value:uint):void{ this._second=value; } public function clone():Time{ return new Time(this.hour,this.minute,this.second); } } }
View Code
package mvc { import flash.events.Event; import flash.events.EventDispatcher; public class ClockData extends EventDispatcher { private var _time:Time public function ClockData() { } public function get time():Time{ if(_time==null){ var date:Date = new Date(); return new Time(date.hours,date.minutes,date.seconds); }else{ return _time.clone(); } } public function set time(value:Time){ _time=value.clone(); dispatchEvent(new Event(Event.CHANGE)); } } }
View Code
package mvc { import flash.display.Sprite; import flash.events.Event; public class AbstractClock extends Sprite { protected var _data:ClockData; public function AbstractClock(data:ClockData){ _data=data; _data.addEventListener(Event.CHANGE, draw); } protected function draw(event:Event=null):void{ } } }
View Code
package mvc { import flash.display.Sprite; import flash.events.Event; public class AnalogClock extends AbstractClock { private var _face:Sprite; private var _hourHand:Sprite; private var _minuteHand:Sprite; private var _secondHand:Sprite; public function AnalogClock(data:ClockData) { super(data); _face = new Sprite(); _face.graphics.lineStyle(0,0x000000,1); _face.graphics.drawCircle(0,0,100); this.addChild(_face); _hourHand = new Sprite(); _hourHand.graphics.lineStyle(5,0x000000,1); _hourHand.graphics.lineTo(0,-50); this.addChild(_hourHand); _minuteHand = new Sprite(); _minuteHand.graphics.lineStyle(2,0x000000,1); _minuteHand.graphics.lineTo(0,-80); this.addChild(_minuteHand); _secondHand = new Sprite(); _secondHand.graphics.lineStyle(0,0x000000,1); _secondHand.graphics.lineTo(0,-80); this.addChild(_secondHand); draw(null); } override protected function draw(event:Event=null):void{ var time:Time = _data.time; _hourHand.rotation=30*time.hour+30*time.minute/60; _minuteHand.rotation=6 * time.minute+6 * time.second/60; _secondHand.rotation=6 * time.second; } } }
View Code
package mvc { import flash.display.Sprite; import flash.events.Event; import flash.text.TextField; import flash.text.TextFieldAutoSize; public class DigitalClock extends AbstractClock { private var _display:TextField; public function DigitalClock(data:ClockData) { super(data); _display = new TextField(); _display.width=50; _display.height=10; _display.autoSize=TextFieldAutoSize.RIGHT; _display.x=1; _display.y=1; this.addChild(_display); draw(null); } override protected function draw(event:Event=null):void{ var time:Time = _data.time; _display.htmlText = "<front face='_typewriter' size='40' >" + time.hour+":"+ zeroFill(time.minute)+":"+zeroFill(time.second)+"</front>"; trace(_display.text); } private function zeroFill(value:Number):String{ if(value>9){ return value.toString(); }else{ return "0"+value.toString(); } } } }
View Code
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.TimerEvent; import flash.utils.Timer; import mvc.AbstractClock; import mvc.AnalogClock; import mvc.ClockData; import mvc.DigitalClock; import mvc.Time; public class TestMVC extends Sprite { private var _clockData:ClockData; public function TestMVC() { stage.align=StageAlign.TOP_LEFT; stage.scaleMode=StageScaleMode.NO_SCALE; _clockData=new ClockData(); var clock1:AbstractClock = new AnalogClock(_clockData); clock1.x=100; clock1.y=100; this.addChild(clock1); var clock2:AbstractClock = new DigitalClock(_clockData); clock2.x=120; clock2.y=200; this.addChild(clock2); var ticker:Timer = new Timer(1000); ticker.addEventListener(TimerEvent.TIMER, onTick); ticker.start(); } public function onTick(evt:TimerEvent):void { var date:Date = new Date(); _clockData.time = new Time(date.hours,date.minutes,date.seconds); } } }