)
* It starts a timer and adds an eventlistener to the timer, as well as setting some initial values
* for some things.
* **/
protected function init():void
{
lbl_debug.visible = debug;
t=new Timer(TIME_UPDATES); // tick every TIME_UPDATES
currTime = new Date(2013,10,7,5,47,0,0); // set default time
alarm = false; // set default alarm mode
t.addEventListener(TimerEvent.TIMER,go); // call 'go' when the timer starts
t.start(); // start the timer
}
/**
* go function is called every time the timer ticks.
* It updates the displayed time, checks if we should set off
* an alarm, and turns off an alarm after 5 seconds.
* **/
protected function go(event:TimerEvent):void
{
currTime.milliseconds += TIME_UPDATES; // increment current Time by 'TIME_UPDATES' number of ms
lbl_timedisplay.text = currTime.getHours() + ":" + currTime.getMinutes() + ":" +currTime.getSeconds(); // update the gui
if (debug) // only run this if we're debugging
lbl_debug.text = "("+alarm+") " + alarmTime.toString() + " vs " + currTime.toString() + " = " + (alarmTime==currTime); // debug line
// If the alarm is on, check the alarm time against our current time
if (alarm && alarmTime != null && alarmTime.valueOf() == currTime.valueOf()) {
lbl_alarm.visible = true; // turn our alarm on
alarmDuration = 0; // restart how long our alarm's been beeping
}
if (alarm && alarmDuration > (5*TIME_UPDATES)) { // turn alarm off after 5 seconds
alarmDuration = 0; // restart how long alarm's been beeping
lbl_alarm.visible = false; // remove alarm
} else if (alarm) {
alarmDuration += TIME_UPDATES; // increment our alarm duration, only if our alarm is still on
}
}
/**
* setAlarm function is called when the setalarm button is pressed.
* It keeps track of whether we're in time/set/alarm mode and switches between
* the modes as needed.
* **/
protected function setAlarm(event:MouseEvent):void
{
if (mode==0 || mode ==1) { // time mode -> alarmset mode
mode = 2;
btn_alarmOn.visible=false;
lbl_mode.text="alarm";
ta_setalarm.visible=true; // make alarm on/off button not visible
}
else if (mode ==2 ) { // alarmset mode -> time mode
mode = 0;
lbl_mode.text="time";
ta_setalarm.visible=false;
btn_alarmOn.visible=true; // make alarm on/off button reappear
alarmTime = new Date(currTime); // set alarm time to be the same as our current time, THEN modify
// the year/month/date needs to be the same so that the alarm & currTime truly are the same
var results:Array = ta_setalarm.text.split(":"); // split the text in the textinput by colons
alarmTime.hours = results[0]; // hours is located in first index
alarmTime.minutes = results[1]; // minutes in second index
if (results.length > 2) // if we were given seconds, then they'll be in index 2
alarmTime.seconds = results[2];
if (results.length > 3) // if we were given milliseconds, they'll be in index 3
alarmTime.milliseconds = results[3];
}
}
/**
* alarmOn function is called when the alarm on/off button is pressed.
* It switches whether the alarm is on or off, and if it's on and beeping
* you can use this button to prematurely (i.e., before 5 seconds) turn
* the bepping off.
* **/
protected function alarmOn(event:MouseEvent):void
{
alarm = !alarm; // switch whether alarm is on/off
if (alarm) {// if it's on, update the GUI to say so
btn_alarmOn.label="Alarm On";
btn_alarmOn.emphasized = true;
} else { // if it's not on, update the GUI to say so
btn_alarmOn.label="Alarm Off";
btn_alarmOn.emphasized = false;
lbl_alarm.visible = false; // turn off our alarm if it's beeping
}
}
]]>