Actionscript 3.0 事件机制剖析--自定义事件实现

  Actionscript 3.0 事件机制剖析

                                               --自定义事件实现

    

     接着上一篇,用一个实例来说明如何自定义一个Actionscript 3的事件。在这个事件中实现了闹钟功能,当时间到的时候,发送一个TIME_ALARM事件,闹钟响应,并提示消息。代码实现如下:

 package { /** * Written by Leezhm, 21st February, 2009 * Contact : [email protected] * Last Modified by Leezhm on 22nd February, 2009 **/ import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; public class TestEvent extends Sprite { private var _timerClock:Timer; public function TestEvent():void { if (stage) { InitApplication(); } else { this.addEventListener(Event.ADDED_TO_STAGE, InitApplication); } } private function InitApplication():void { this.removeEventListener(Event.ADDED_TO_STAGE, InitApplication); // Add a listener for the TIME_ALARM event. this.addEventListener(AlarmEvent.TIME_ALARM, OnAlarmClock); this._timerClock = new Timer(0, 1); this._timerClock.addEventListener(TimerEvent.TIMER, OnTimerClock); SetAlarmClock(11, 15);//测试时间为2009-02-23 11:15:00 } /** * Dispatch the TIME_ALARM event when the currrent time was be * the alarm setting time */ private function OnTimerClock(evt:TimerEvent):void { var _alarmEvent:AlarmEvent = new AlarmEvent(); _alarmEvent.message = "Please get up!!!It's time for breakfast!!!"; this.dispatchEvent(_alarmEvent); } /** * the event handling function of TIME_ALARM. */ private function OnAlarmClock(evt:AlarmEvent):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._timerClock.reset(); this._timerClock.delay = _alarmClock.time - _now.time; this._timerClock.start(); } } } }

     AlarmEvent事件是继续了flash.events.Event事件,在此事件中添加了新的属性message,它是当闹钟时间到时的提示信息。在继承flash.events.Event事件的子类中必须override父类的clone方法,同样也应该继承toString方法。下面给出一张Adobe的官方文档Programming ActionScript 3.0中关于继承Event事件的说明图,如下:

 

Actionscript 3.0 事件机制剖析--自定义事件实现_第1张图片

flash.events.Event.Clone()

flash.events.Evnet.toString()

 

  关于本例中AlarmEvent的实现如下:

 package { /** * Written by Leezhm, 21st February, 2009 * Contact : [email protected] * Last Modified by Leezhm on 22nd February, 2009 **/ import flash.events.Event; public class AlarmEvent extends Event { public static const TIME_ALARM:String = "timealarm"; private var _messages:String = ""; public function AlarmEvent():void { super(TIME_ALARM); } public function set message(msg:String):void { if ("" != msg) { this._messages = msg; } } public function get message():String { return this._messages; } public override function clone():Event { var _tmpAlarm:AlarmEvent = new AlarmEvent(); _tmpAlarm._messages = this._messages; return _tmpAlarm; } public override function toString():String { return this.formatToString("AlarmEvent", "type", "bubbles", "cancelable", "eventPhase", "message"); } } }

 

fla文件可以自己建立,上面不需要添加任何元件,关联到TestEvent类即可,然后设置闹钟时间。运行结果截图如下:

Actionscript 3.0 事件机制剖析--自定义事件实现_第2张图片

 

你可能感兴趣的:(Actionscript 3.0 事件机制剖析--自定义事件实现)