Actionscript 3.0 事件机制剖析
事件发送方式(二)
声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。
本文链接:http://blog.youkuaiyun.com/li_007/archive/2009/03/02/3949812.aspx
开拓进取的小乌龟------->优快云点滴点点滴滴Blog
关于复合EventDispatcher class的方法很简单,就是在要发送事件的class中声明一个EventDispatcher的对象,然后在要发送Event的地方调用EventDispatcher实例对象的dispatchEvent方法就OK。具体实现如下:
1、AlarmEvent class不做任何改变。
2、测试文档类实现如下: package { import flash.display.Sprite; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.TimerEvent; import flash.utils.Timer; /** * Written by Leezhm, 28th February, 2009 * Contact : Leezhm@126.com * Last Modified by Leezhm on 2nd March, 2009 **/ public class Alarm extends Sprite { private var _timer:Timer; private var _dispatcher:EventDispatcher; public function Alarm():void { InitTimer(); } private function InitTimer():void { if (null == this._timer) { this._timer = new Timer(0, 1); } this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, OnTimerComplete); SetAlarmClock(16, 49); } private function OnTimerComplete(evt:TimerEvent = null):void { if (null == this._dispatcher) { this._dispatcher = new EventDispatcher(); this._dispatcher.addEventListener(AlarmEvent.TIME_ALARM, OnAlarm); } var _alarm:AlarmEvent = new AlarmEvent(); _alarm.message = "Please get up!!!It's time for breakfast!!!"; this._dispatcher.dispatchEvent(_alarm); } private function OnAlarm(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(); } } } }
顺便贴上测试结果截图:
在上面的Alarm class中复合了一个EventDispatcher实例对象。
