Actionscript 3.0 事件機制剖析—事件發送方式(三)

來源:互聯網
上載者:User
     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>}

好了,測試後結果為:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.