AS3学习之数字时钟

2007-01-04 21:09:39

从N神那搞来本文档.随便看看.随手也做了个FLASH时钟,体验一下MVC模型的思想 详情见书 [url=http://flash.nshen.net/Adobe.Press.Advanced.ActionScript.3.with.Design.Patterns.Nov.2006.chm]Adobe.Press.Advanced.ActionScript.3.with.Design.Patterns.Nov.2006.chm[/url] PartII [N神兄,盗一下你的链接不介意吧,空间紧张.呵呵] 学习嘛,建上和书中说的目录结构 mvcexample/data mvcexample/clock 下一部,当然是抄代码了,哇哈哈,抄代码,我最爱了 Time.as //封装一下时间数据,方便提取了.[我将其构造函数改了下,感觉好用了些.按书中写的,时间老是动不了,详情总是见 ClockTest.as] [code] package mvcexample.data { public class Time { private var _hour:uint; private var _minute:uint; private var _second:uint; public function get hour():uint { return _hour; } public function set hour(value:uint):void { _hour = value; } public function get minute():uint { return _minute; } public function set minute(value:uint):void { _minute = value; } public function get second():uint { return _second; } public function set second(value:uint):void { _second = value; } public function Time(...arg) { if(arg.length<=0){ var date:Date = new Date(); _hour = date.hours; _minute = date.minutes; _second = date.seconds; //return new Time(date.hours, date.minutes, date.seconds); }else{ _hour = arg[0]; _minute = arg[1]; _second =arg[2]; } } public function clone():Time { return new Time(_hour, _minute, _second); } } } [/code] ClockData.as //这个类感觉是增加一个触发器类的东东,当修改时间.触发Event.CHANGE事件 [code] package mvcexample.data { import flash.events.Event; import flash.events.EventDispatcher; import mvcexample.data.Time; public class ClockData extends EventDispatcher { private var _time:Time; 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):void { _time = value.clone(); trace("Dispath Event"); dispatchEvent(new Event(Event.CHANGE)); } } } [/code] 然后在 /clock/中放 AbstractClockView.as 一个抽像类 [code]package mvcexample.clock { import flash.display.Sprite; import mvcexample.data.ClockData; import mvcexample.data.Time; import flash.events.Event; public class AbstractClockView extends Sprite { private var _face:Sprite; protected var _data:ClockData; public function AbstractClockView(data:ClockData) { _data = data; _data.addEventListener(Event.CHANGE, draw); } protected function draw(event:Event):void { trace("Draw...."); } //下面仅供测试,看一下广播了 Event.Change事件没,呵呵 // public function setTime(t:Time){ // _data.time = t; // } } } [/code] AnalogClock.as 呵呵,这个类被我改了许多...因为原文根本不能发布,就是构造那一块中的 draw(),根本少参数. [code] package mvcexample.clock { import mvcexample.data.ClockData; import mvcexample.data.Time; import flash.display.Sprite; import flash.events.Event; public class AnalogClock extends AbstractClockView { 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); addChild(_face); _hourHand = new Sprite(); _hourHand.graphics.lineStyle(5, 0x000000, 1); _hourHand.graphics.lineTo(0, -50); addChild(_hourHand); _minuteHand = new Sprite(); _minuteHand.graphics.lineStyle(2, 0x000000, 1); _minuteHand.graphics.lineTo(0, -80); addChild(_minuteHand); _secondHand = new Sprite(); _secondHand.graphics.lineStyle(0, 0x000000, 1); _secondHand.graphics.lineTo(0, -80); addChild(_secondHand); draw(null); } override protected function draw(event:Event):void { var time:Time = _data.time; // Set the rotation of the hands based on the time // values. _hourHand.rotation = 30 * time.hour + 30 * time.minute / 60; _minuteHand.rotation = 6 * time.minute + 6 * time.second / 60; _secondHand.rotation = 6 * time.second; } } } [/code] 下面就是视图部分 ClockTest.as ,就放在mvcexample的父级目录吧 看一下 onTimer事件接口.以前里面是_clockData.time = new Time(5,1,1); 时间老是在 5点1分, 现在 _clockData.time = new Time();这样时间就是当前时间了,因为 Time.as的构造,无参就读取默认时间,有参就设置自定义时间.呵呵 [code]package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import mvcexample.data.ClockData; import mvcexample.clock.AbstractClockView; import mvcexample.clock.AnalogClock; import mvcexample.data.Time; import flash.utils.Timer; import flash.events.TimerEvent; public class ClockTest extends Sprite { private var _clockData:ClockData; public function ClockTest() { //stage.align = StageAlign.TOP_LEFT; //stage.scaleMode = StageScaleMode.NO_SCALE; _clockData = new ClockData(); var clock:AbstractClockView = new AnalogClock(_clockData); clock.x = 100; clock.y = 100; addChild(clock); var timer:Timer = new Timer(2000, 0); timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start(); trace(timer); } private function onTimer(event:TimerEvent){ _clockData.time = new Time(); trace("Timer..."); } } } [/code] 效果如下 [swf]attachments/month_0701/96xb_ClockTest.swf[/swf]