Actionscript 3.0 事件機制剖析 事件發送方式(三)
聲明:歡迎任何人和組織轉載本blog中文章,但必須標記文章原始連結和作者資訊。 本文連結:http://blog.csdn.net/li_007/archive/2009/03/02/3951003.aspx 開拓進取的小烏龜------->CSDN點滴點點滴滴Blog
關於事件發送方式三,肯定是實現IEventDispatcher Interface了。其實EventDispatcher class就是實現了IEventDispatcher Interface的,IEventDispatcher Interface聲明了5個事件發送函數,關於IEventDispatcher的描述如下:
由上面的描述,可以看出,IEventDispatcher Interface的執行還是需要EventDispatcher class 對象的,所以在implements IEventDispatcher Interface的時候,還是要聲明一個EventDispatcher的對象,讓它來具體實現IEventDispatcher Interface的五個方法。具體實現如下:
1、AlarmEvent class不做任何改變。
2、AlarmClock class實現如下:
package<br />{<br />import flash.events.Event;<br />import flash.events.EventDispatcher;<br />import flash.events.IEventDispatcher;</p><p>/**<br /> * Written by Leezhm, 1st March, 2009<br /> * Contact : Leezhm@126.com<br /> * Last Modified by Leezhm on 2nd March, 2009<br /> **/</p><p>public class AlarmClock implements IEventDispatcher<br />{<br />private var _dispatcher:EventDispatcher;</p><p>public function AlarmClock():void<br />{<br />InitAlarmClock();<br />}</p><p>private function InitAlarmClock():void<br />{<br />if (null == this._dispatcher)<br />{<br />this._dispatcher = new EventDispatcher(this);<br />}<br />}</p><p>/**<br /> * all function of IEventDispatcher Interface<br /> */<br />public function addEventListener(type:String, listener:Function,<br /> useCapture:Boolean = false, priority:int = 0,<br /> useWeakReference:Boolean = false):void<br />{<br />this._dispatcher.addEventListener(type, listener, useCapture,<br /> priority, useWeakReference);<br />}</p><p>public function dispatchEvent(evt:Event):Boolean<br />{<br />return this._dispatcher.dispatchEvent(evt);<br />}</p><p>public function hasEventListener(type:String):Boolean<br />{<br />return this._dispatcher.hasEventListener(type);<br />}</p><p>public function removeEventListener(type:String, listener:Function,<br /> useCapture:Boolean = false):void<br />{<br />this._dispatcher.removeEventListener(type, listener, useCapture);<br />}</p><p>public function willTrigger(type:String):Boolean<br />{<br />return this._dispatcher.willTrigger(type);<br />}<br />}</p><p>}
3、TestAlarmClock class實現如下:
package<br />{<br />import flash.display.Sprite;<br />import flash.events.TimerEvent;<br />import flash.utils.Timer;<br />import flash.events.Event;</p><p>/**<br /> * Written by Leezhm, 1st March, 2009<br /> * Contact : Leezhm@126.com<br /> * Last Modified by Leezhm on 2nd March, 2009<br /> **/</p><p>public class TestAlarmClock extends Sprite<br />{<br />private var _timer:Timer;</p><p>public function TestAlarmClock():void<br />{<br />InitApplication();<br />}</p><p>private function InitApplication():void<br />{<br />if (null == this._timer)<br />{<br />this._timer = new Timer(0, 1);<br />this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, OnAlarmClockComplete);<br />}</p><p>SetAlarmClock(23, 34);<br />}</p><p>private function OnAlarmClockComplete(evt:TimerEvent = null):void<br />{<br />var _alarmEvent:AlarmEvent = new AlarmEvent();<br />_alarmEvent.message = "Please get up!!!It's time for breakfast!!!";</p><p>var _alarmClock:AlarmClock = new AlarmClock();<br />_alarmClock.addEventListener(AlarmEvent.TIME_ALARM, OnAlarmClock);<br />_alarmClock.dispatchEvent(_alarmEvent);<br />}</p><p>private function OnAlarmClock(evt:AlarmEvent = null):void<br />{<br />trace("Alarm!!!");</p><p>var _alarmClone:Event = evt.clone();<br />trace(_alarmClone);<br />}</p><p>/**<br /> * Sets the time at which the alarm should go off.<br /> * @param hour The hour portion of the alarm time.<br /> * @param minutes The minutes portion of the alarm time.<br /> */<br />private function SetAlarmClock(hour:uint, minutes:uint):void<br />{<br />var _now:Date = new Date();<br />var _alarmClock:Date = new Date(_now.fullYear, _now.month, _now.date,<br /> hour, minutes);</p><p> if (_alarmClock <= _now)<br />{<br />trace("Error time! Now is " + _now.toLocaleDateString());<br />}<br />else<br />{<br />//reset the timer;<br />this._timer.reset();<br />this._timer.delay = _alarmClock.time - _now.time;<br />this._timer.start();<br />}<br />}<br />}</p><p>}
好了,測試後結果為: