Está en la página 1de 5

RELOJ CON ALARMA

La función principal de este reloj, es el control del tiempo y la visualización el reloj,


añadiendo una funcionalidad, creación de una alarma, incluyendo ajustes a la hora de
alarma y la notificación de cuando la alarma suene.

ACTIVACIÓN DE LA ALARMA:
Usaremos AlarmClock, para la configuración y activación de la alarma y Timer, para
determinar cuándo activar dicha alarma.

-Activación de la alarma:
import flash.events.TimerEvent;
import flash.utils.Timer;

/**
* The Timer that will be used for the alarm.
*/
public var alarmTimer:Timer;
...
/**
* Instantiates a new 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);
}

- Establecer alarma
/**
* 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;
}

DISTRIBUCIÓN DEL EVENTO:


Una vez especificado el tiempo, se producirá un detector de evento, el cual se llama OnAlarm

/**
* Called when the timer event is dispatched.
*/
public function onAlarm(event:TimerEvent):void
{
trace("Alarm!");
var alarm:AlarmEvent = new AlarmEvent(this.alarmMessage);
this.dispatchEvent(alarm);
}

NOTIFICACIÓN DE LA ALARMA

var alarm:AlarmEvent = new AlarmEvent(this.alarmMessage);


this.dispatchEvent(alarm);

CREACIÓN DE UN EVENTO DE ALARMA PERSONALIZADO

import flash.events.Event;

/**
* This custom Event class adds a message property to a basic Event.
*/
public class AlarmEvent extends Event
{
/**
* The name of the new AlarmEvent type.
*/
public static const ALARM:String = "alarm";

/**
* A text message that can be passed to an event handler
* with this event object.
*/
public var message:String;

/**
*Constructor.
*@param message The text to display when the alarm goes off.
*/
public function AlarmEvent(message:String = "ALARM!")
{
super(ALARM);
this.message = message;
}
...
}

/**
* Creates and returns a copy of the current instance.
* @return A copy of the current instance.
*/
public override function clone():Event
{
return new AlarmEvent(message);
}

/**
* Returns a String containing all the properties of the current
* instance.
* @return A string representation of the current instance.
*/
public override function toString():String
{
return formatToString("AlarmEvent", "type", "bubbles", "cancelable", "eventPhase",
"message");
}

También podría gustarte