sign up!

Are you interested in learning, then sign up to be notified about new tutorials.

Email:


helpfull links


website solutions

Do you need a website?
We can have you online in 24 hrs for less then $100


master flash

Are you looking to learn flash? If so you need to check out Flash, the Information you need. You will find all of these tutorials, source files and more...

Get your copy today!

Flash, This Information you need

a few friends


follow Us On

FrenchSquared on Linked in

FrenchSquared on Twitter

Frenchsquared on Facebook



Flash How To: Alarm Clock

So, there are times when you want an event to happen at a certain time or based on the time of day. But what about if you want something to happen at a time set by the user? For instance if you are working on a messaging program that sends an email out to a user at a set time. Or perhaps you are creating a game and the user has the ability to set their own time limit. Then, this flash actionscript tutorial is for you. In this flash actionscript tutorial you will learn how to create a rather simple Alarm Clock.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Flash: Get the time

Once you have the starting file open, press F9 to open the action’s panel. The first thing that needs to be done is to get the time. So, add the 5 variables from the example code. You should be familiar with these methods. Flash is simply getting the Date and assigning it to the variable time. The next three variables are accessing the individual pieces of time. Specifically, the hours, minutes and seconds. The last variable, goingOff is a boolean that will be used latter on, to detect if the alarm is going of or not.

var time = new Date();
var seconds = time.getSeconds()
var minutes = time.getMinutes()
var hours = time.getHours()
var goingOff:Boolean

In ActionScript 3.0 there needs to be an event listener and its function. So, copy the example code to create an ENTER_FRAME listener. This event listener will up date the time every time the keyframe is reloaded. Inside the function clock, you will add three lines of code that are simple going to set three corresponding text field as equal to the variables you defined in the first few lines of code. The text fields have been created for you and added to the stage. Basically, a digital clack has been created, but if you were to test the movie at this time you would see that the time is displayed in military time.

stage.addEventListener(Event.ENTER_FRAME, clock)
function clock(event:Event):void{

sec_txt.text = seconds;
min_txt.text = minutes;
hour_txt.text = hours;

};

You may not want military time so an if statement need to be created to check to see if the hour is past twelve. If it is past twelve subtract 12 from the current hour. Simple paste the example if statement above the event listener.

if (hours >= 13){
hours = hours - 12
};

The clock still doesn’t look quite correct. If you notice that when the hour, minutes or seconds is less then 10 there is a blank space where you would normally see a zero. The easiest way I have found to correct this is to make a movie clip of a zero and turn its visibility on or off as needed. These if statements are very basic. The are looking to see if the variables hours, minutes or seconds is less then or equal to 10. If so make the corresponding movie clip visible. If the variables are not less then or equal to 9 then make the corresponding movie clips not visible. The three movie clips Hzero_mc, Mzero_mc and Szero_mc were created for you and simple contain a zero.

if (hours <= 9){

Hzero_mc.visible = true

else {

Hzero_mc.visible = false

}

 

if (minutes <= 9){

Mzero_mc.visible = true

else {

Mzero_mc.visible = false

}

 

if (seconds <= 9){

Szero_mc.visible = true

else {

Szero_mc.visible = false

}

Two more variables need to be created. These variables are going to be used to store the time the user places inside two input text fields, hour_in and minute_in. The variables are going to be for the minutes and hours that the alarm is going to go off. Seconds will just default to zero, although you could add a variable for them if you wish.

var alarmHour = hour_in.text
var alarmMin = minute_in.text

The set alarm button has been created and is on the stage. This is simple a button that is going to listen for a mouse click. When the mouse is clicked the function setAlarm will set the variables alarmHour and alarmMin to be equal to the value in the two input text field.

setAlarm_btn.addEventListener(MouseEvent.CLICK, setAlarm);
function setAlarm(event:MouseEvent):void{

alarmHour = hour_in.text
alarmMin = minute_in.text

};

So far, a clock, set button and variables for the alarm have been created. But, what to do with these objects. A statement simply needs to be created to see if the current time is equal to the variables defined by the user. Don’t worry this code looks worse then it actual is. Another event listener is being add to the stage that will call the function checkAlarm every time the frame is loaded. Inside the function flash is looking to see if hours is equal to alarmHour (remember the user variables), and minutes is equal to alarmMin and seconds are equal to zero. All these statements have to be true for Flash to continue inside the if statement. If even one of the statements are false flash will not perform the rest of the function. If they are all true Flash is going to check one more thing. Remember the boolean variable goingOff that was created in the beginning. That variable is going to be used to make certain the sound of the alarm is only played once. If the sound isn’t playing, set the variable goingOff as true, so we know the sound is playing. Next, create a new instance of the sound “Ding”. Then start playing the sound. The next time the function checkAlarm is found true, Flash will see that the boolean going of is true and not start the alarm again.

stage.addEventListener(Event.ENTER_FRAME, checkAlarm)
function checkAlarm (event:Event):void{

if (hours == alarmHour && minutes == alarmMin && seconds == 0){
if (goingOff == false){
goingOff = true
var alarm:Sound = new Ding
alarm.play();
}
}

};

The alarm clock will currently work, but those experienced with Flash will notice that currently the isn’t away to set the boolean goingOff back to false. That means the alarm will only work one time. This is a simple fix, just add the last line of example code inside the function setAlarm. That way every time the alarm is set the boolean goingOff will also be reset.

goingOff = false;

Press Control-Enter and begin playing with you new alarm clock, if the seconds on your clock are not changing it is because you may not have atleast two key frames on the time line. You need a blank key frame after the clock, so that flash will reload the clock key frame.

Download Tutorials