Actionscript 3.0 事件机制剖析---事件发送方式(三)

     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 { 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(); } } } }

好了,测试后结果截图为:

Actionscript 3.0 事件机制剖析---事件发送方式(三)_第1张图片

你可能感兴趣的:(timer,String,function,Class,interface,actionscript)