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

 

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

顺便贴上测试结果截图:

 

ClockAlarm—Test   

 

在上面的Alarm class中复合了一个EventDispatcher实例对象。 

转载于:https://www.cnblogs.com/leezhm/archive/2009/03/02/2560330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值