topical media & game development

talk show tell print

actionscript-book-AlarmClock-com-example-programmingas3-clock-AlarmClock.ax

actionscript-book-AlarmClock-com-example-programmingas3-clock-AlarmClock.ax [swf] flex


  package 
  {
          public class @ax-actionscript-book-AlarmClock-com-example-programmingas3-clock-AlarmClock extends actionscript_book_AlarmClock_com_example_programmingas3_clock_SimpleClock 
          {
                  //import com.example.programmingas3.clock.AnalogClockFace; 
                  import flash.events.TimerEvent;
                  import flash.utils.Timer;
                          
                  public var alarmTime:Date;
                  
                  public var alarmMessage:String;
                  
                  
The Timer that will be used for the alarm.

  
                  public var alarmTimer:Timer;
                  
                  public static var MILLISECONDS_PER_DAY:Number = 1000 * 60 * 60 * 24;
                  
                  
Instantiates a new @ax-actionscript-book-AlarmClock-com-example-programmingas3-clock-AlarmClock of a given size

  
                  public override function initClock(faceSize:Number = 200):void
                  {
                          super.initClock(faceSize);
  
                          alarmTimer = new Timer(0, 1);
                  alarmTimer.addEventListener(TimerEvent.TIMER, onAlarm);
          }
  
          
Sets the time at which the alarm should go off.
parameter: hour The hour portion of the alarm time
parameter: minutes The minutes portion of the alarm time
parameter: message The message to display when the alarm goes off.
returns: The time at which the alarm will go off.

  
          public function setAlarm(hour:Number = 0, minutes:Number = 0, message:String = "Alarm!"):Date
          {
                  this.alarmMessage = message;
                  
                   var now:Date = new Date();
                  
                  // create this time on today's date
                  alarmTime = new Date(now.fullYear, now.month, now.date, hour, minutes);
                  
                  // determine if the specified time has already passed today
                  if (alarmTime <= now)
                  {
                          alarmTime.setTime(alarmTime.time + MILLISECONDS_PER_DAY);
                  }
                  
                  // reset the alarm timer if it's currently set
                  alarmTimer.reset();
                          // calculate how many milliseconds should pass before the alarm should
                          // go off (the difference between the alarm time and now) and set that
                          // value as the delay for the alarm timer
                          alarmTimer.delay = Math.max(1000, alarmTime.time - now.time);
                          alarmTimer.start();
                  
                          return alarmTime;
          }
  
                  
Called when the Timer event is received

  
                  public function onAlarm(event:TimerEvent):void 
                  {
                          trace("Alarm!");
                          var alarm:actionscript_book_AlarmClock_com_example_programmingas3_clock_AlarmEvent = new actionscript_book_AlarmClock_com_example_programmingas3_clock_AlarmEvent(this.alarmMessage);
                          this.dispatchEvent(alarm);
                  }
          }
  }


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.