Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 367

Programming actionscript 3.0
Table of Contents

Advertisement

The Timer instance defined in the AlarmClock class is named
method, which performs necessary setup operations for the AlarmClock
initClock()
instance, does two things with the
parameters instructing the Timer instance to wait 0 milliseconds and only trigger its timer
event one time. After instantiating
addEventListener()
A Timer instance works by dispatching its
passed. The AlarmClock class will need to know when the
to set off its own alarm. By calling
as a listener with
alarmTimer
to listen for the
timer
the event happens, the AlarmClock class's
the event.
In order to actually set the alarm, the AlarmClock class's
follows:
/**
* 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.
* @param message The message to display when the alarm goes off.
* @return 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);
}
// Stop 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;
}
alarmTimer
alarmTimer
method to indicate that it wants to listen to that variable's
timer
addEventListener()
. The two parameters indicate that the AlarmClock class wants
event (indicated by the constant
onAlarm()
alarmTimer
variable. First, the variable is instantiated with
, the code calls that variable's
event after a specified amount of time has
event is dispatched in order
timer
, the AlarmClock code registers itself
TimerEvent.TIMER
method should be called in response to
setAlarm()
Example: Alarm Clock
. The
event.
timer
), and that when
method is called, as
367

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents