关于事件发送方式三,肯定是实现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 { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; /** * Written by Leezhm, 1st March, 2009 * Contact : [email protected] * Last Modified by Leezhm on 2nd March, 2009 **/ public class AlarmClock implements IEventDispatcher { private var _dispatcher:EventDispatcher; public function AlarmClock():void { InitAlarmClock(); } private function InitAlarmClock():void { if (null == this._dispatcher) { this._dispatcher = new EventDispatcher(this); } } /** * all function of IEventDispatcher Interface */ public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { this._dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); } public function dispatchEvent(evt:Event):Boolean { return this._dispatcher.dispatchEvent(evt); } public function hasEventListener(type:String):Boolean { return this._dispatcher.hasEventListener(type); } public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { this._dispatcher.removeEventListener(type, listener, useCapture); } public function willTrigger(type:String):Boolean { return this._dispatcher.willTrigger(type); } } }
3、TestAlarmClock class实现如下:
package { import flash.display.Sprite; import flash.events.TimerEvent; import flash.utils.Timer; import flash.events.Event; /** * Written by Leezhm, 1st March, 2009 * Contact : [email protected] * Last Modified by Leezhm on 2nd March, 2009 **/ public class TestAlarmClock extends Sprite { private var _timer:Timer; public function TestAlarmClock():void { InitApplication(); } private function InitApplication():void { if (null == this._timer) { this._timer = new Timer(0, 1); this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, OnAlarmClockComplete); } SetAlarmClock(23, 34); } private function OnAlarmClockComplete(evt:TimerEvent = null):void { var _alarmEvent:AlarmEvent = new AlarmEvent(); _alarmEvent.message = "Please get up!!!It's time for breakfast!!!"; var _alarmClock:AlarmClock = new AlarmClock(); _alarmClock.addEventListener(AlarmEvent.TIME_ALARM, OnAlarmClock); _alarmClock.dispatchEvent(_alarmEvent); } private function OnAlarmClock(evt:AlarmEvent = null):void { trace("Alarm!!!"); var _alarmClone:Event = evt.clone(); trace(_alarmClone); } /** * Sets the time at which the alarm should go off. * @param hour The hour portion of the alarm time. * @param minutes The minutes portion of the alarm time. */ private function SetAlarmClock(hour:uint, minutes:uint):void { var _now:Date = new Date(); var _alarmClock:Date = new Date(_now.fullYear, _now.month, _now.date, hour, minutes); if (_alarmClock <= _now) { trace("Error time! Now is " + _now.toLocaleDateString()); } else { //reset the timer; this._timer.reset(); this._timer.delay = _alarmClock.time - _now.time; this._timer.start(); } } } }
好了,测试后结果截图为: