大家好,我是奶綠茶
Countdown在 flash 的專案裡,應該也是很常出現的吧
所以我就把他包裝成一個 class , 方便使用
預設是抓取 client 端的時間
如果想要以伺服器時間為主的話
也只要使用 URLLoader 透過伺服器程式回傳時間
再更新我所寫的 Countdown類別可以。
Countdown.as
Countdown在 flash 的專案裡,應該也是很常出現的吧
所以我就把他包裝成一個 class , 方便使用
預設是抓取 client 端的時間
如果想要以伺服器時間為主的話
也只要使用 URLLoader 透過伺服器程式回傳時間
再更新我所寫的 Countdown類別可以。
Countdown.as
/** * @author milkmidi * @see http://milkmidi.blogspot.com * @version 1.0.1 * @date created 2010/11/16/ */ package milkmidi.utils { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.TimerEvent; import flash.utils.Timer; [Event(name = "countDownComplete" , type = "milkmidi.utils.Countdown")] [Event(name = "countDownUpdate" , type = "milkmidi.utils.Countdown")] public class Countdown extends EventDispatcher { public static const COUNTDOWN_COMPLETE :String = "countDownComplete"; public static const COUNTDOWN_UPDATE :String = "countDownUpdate"; private static const SEC :uint = 1000; private static const MIN :uint = SEC * 60; private static const HOUR :uint = MIN * 60; private static const DAY :uint = HOUR * 24; private var _endYear :uint; private var _endMonth :uint; private var _endDay :uint; private var _endHour :uint; private var _endMin :uint; private var _endDate :Date; // private var _year :int; private var _month :int; private var _day :int; private var _hour :int; private var _min :int; private var _sec :int; private var _timer :Timer = new Timer(1000); private var _today :Date; /** * * @param pYear * @param pMonth * @param pDay * @param pHour * @param pMin */ public function Countdown( pYear:uint, pMonth:uint , pDay:uint , pHour:uint = 0, pMin:uint = 0 ) { _endYear = pYear ; _endMonth = pMonth - 1; _endDay = pDay; _endHour = pHour; _endMin = pMin; _endDate = new Date(_endYear, _endMonth , _endDay, _endHour , _endMin); _timer.addEventListener(TimerEvent.TIMER , timerHandler); } /** * * @param pYear * @param pMonth * @param pDay * @param pHour * @param pMin */ public function setNewDate(pYear:uint, pMonth:uint , pDay:uint , pHour:uint = 0, pMin:uint = 0):void { _today = new Date(pYear , pMonth - 1 , pDay , pHour , pMin); } public function start():void { _timer.start(); timerHandler(null); } public function stop():void { _timer.stop(); } private function timerHandler(e:TimerEvent):void { var _todayDate:Date; if (_today == null) _todayDate = new Date(); else _todayDate = new Date(_today.getTime() + _timer.currentCount * 1000); var differ :Number = _endDate.getTime() - _todayDate.getTime(); _day = Math.floor(differ / DAY); differ -= _day * DAY; _hour = Math.floor(differ / HOUR); differ -= _hour * HOUR; _min = Math.floor(differ / MIN); differ -= _min * MIN; _sec = Math.floor(differ / SEC); if (_day < 0) { stop(); _year = _month = _day = _hour = _min = _sec = 0; dispatchEvent(new Event(COUNTDOWN_COMPLETE)); }else { dispatchEvent(new Event(COUNTDOWN_UPDATE)); } } public function destroy():void { stop(); _timer = null; } public function get year() :uint { return _year; } public function get sec() :uint { return _sec; } public function get min() :uint { return _min; } public function get hour() :uint { return _hour; } public function get day() :uint { return _day; } public function get month() :uint { return _month; } override public function toString():String { return "year:" + year + " month:" + month + " day:" + day + " hour:" + hour + " min:" + min + " sec:" + sec; } } }測試:
/** * @mxmlc -o deploy/CountdownDemo.swf -default-size 800 600 -optimize=true -default-frame-rate=36 -default-background-color=0xffffff -debug=false * @author milkmidi * @see http://milkmidi.blogspot.com * @version 1.0.1 * @date created 2010/11/16/ */ package { import flash.display.Sprite; import flash.events.Event; import flash.text.TextField; import milkmidi.utils.Countdown; public class CountdownDemo extends Sprite { private var _tf:TextField; private var _cd:Countdown; public function CountdownDemo() { addChild(_tf = new TextField); _tf.width = 300; //預設是使用 client 端的時間 _cd = new Countdown(2010, 12, 31); _cd.addEventListener(Countdown.COUNTDOWN_UPDATE , _countDownUpDateHandler); _cd.addEventListener(Countdown.COUNTDOWN_COMPLETE , _countDownCompleteHandler); _cd.start(); // 從伺服器抓到正確的時間後, 把時間填寫入函式,即可更新 //_cd.setNewDate(年,月,日,時,分) } private function _countDownCompleteHandler(e:Event):void { _tf.text = "Happy new year"; } private function _countDownUpDateHandler(e:Event):void { _tf.text = _cd.toString(); } } }轉載請註明出處 http://milkmidi.blogspot.com/2010/11/flashas3-countdown.html
留言
[Event(name = "countDownComplete" , type = "milkmidi.utils.Countdown")]
這種寫法GOOGLE關鍵字該查什麼呢?
不明白這種寫法。
謝謝奶綠大~
那段語法的意思是宣告該類別會發出該事件
但加不加都無所謂
如果有加的話
var c:Countdown = new Countdown ();
c.addEventListener( 打到這時,就會出現上方有宣告的 Event 類型
沒加的話,就不會提示。
你可以看到 Flex 的 Source 裡,都有加入這個 Tag