RSS link icon

Make a timer alarm clock 

 

In this Adobe Flash tutorial we will be making a timer alarm counter.

 

This will be the final result, a function that counts in seconds and mili seconds, when we hit a specific number, we will start an alarm function.

 

 

First we need a bit of preparation on the stage before we get to the coding part.

 

Find a image of a digital clock, go to file -> import -> import to stage and select the image.

 

Now Right click and convert it to a movie clip. (this is all just to make it look interesting).

 

Now make a text field on the stage, place it within the display of our clock, then in the properties panel make it a dynamic text field and give it an instance name, I named mine tStatus.

 

actionscript timer function

 

Next we need to make another text field and place it below.

In the properties panel also make it dynamic and give it an instance name, I named mine message_txt, give it a bigger font and make it red.

 

actionscript timer function

 

Now we are ready to go to the actionscript panel and type in some code.

So open up the actionscript panel and type in the following code, I made some code description between the code lines to make it more understandable (I hope).

 

 

// First we make an instance of the timer class and name it timerClock

var timeClock:Timer = new Timer(100);

 

// then we start it when the flash movie starts

timeClock.start();

 

// Here we make couple of variables, the first one handles the timer count

var time:Number = 0

 

// this one we can change to whatever timer we want the alarm to start

// now its set to 5 seconds

var alarmTime:Number = 5;

 

// Now we add an eventlistener to the timer instance

// so we can tricker a function to add a number to our time variable.

timeClock.addEventListener(TimerEvent.TIMER, updateClock);

 

// this is the function trickered by the timer

function updateClock(event:TimerEvent):void

{

 

// Here we have two conditions first one is if the time is more or

// equal to the alarm time, this will make the alarm start

// and the timer stop counting.

if(time >= alarmTime)

{

trace("ALARM!!!");

message_txt.text = "Wake up!!";

timeClock.removeEventListener(TimerEvent.TIMER, updateClock);

}

 

// the else statement tells flash to add .1 to the counter every time

// this function is trickered.

else

{

time += .1

 

// this might seem a bit confusing but Math.round rounds down so we dont

// get any .2332 numbers then we * by 10 to make it in seconds

// and divide it all by 10 to get seconds and milliseconds.

time = (Math.round(time*10))/10;

 

// here we set the timer text field tStatus equal to the time, remember

// to use the String() to convert it to string, or else you will get error

tStatus.text = String(time);

}

}

 

 

 


nigfrog says: Monday, October 27, 2008

i am sayed pande


Martin says: Thursday, June 12, 2008

@Jenny
Two things I suspect may be wrong: first, in the if-statement, it should read if(time=alarmTime), and if you want to skip the greater than, you need two equals signs, otherwise you are assigning the value of alarmTime to time, i.e. the alarm will go off instantly. Second, and this I think is the cause for your error message, where you have message_txt = "Wakeup!", it needs to be message_txt.text =, so you assign the text to the text property of message_txt, which I assume is a text field or something. Hope it helps.


Admin Bob says: Saturday, May 31, 2008

hi Jenny, my comments section does not allow all that code, can you try sending me the flash file via email, support@0tutor.com


Jenny Andrews says: Saturday, May 31, 2008

Not sure what IChr(34)ve done wrong but this didnChr(34)t work for me. IChr(34)m just a beginner so I could have goofed.

HereChr(34)s the code I used.
var timeClock:Timer=new Timer(100);
timeClock.start();
var time:Number=0
var alarmTime:Number=5
timeClock.addEventListener(TimerEvent.TIMER,updateClock);
function updateClock(event:TimerEvent):void
{
if(time=alarmTime)
{
trace("ALARM!!!");
message_txt="Wake up!!";
timeClock.removeEventListener(TimerEvent.TIMER, updateClock);
}
else
{
time+=.1
time=(Math.round(time10))/10;
tStatus.text=String(time);
}
}

and this is the error message I got.

"1067: Implicit coercion of a value of type String to an unrelated type flash.text:TextField."

Any suggestions

Jenny




Admin Bob says: Thursday, May 15, 2008

you party like a rockstar!


Admin Bob says: Saturday, April 05, 2008

hi Marina
you should read my tutorial on music here http://blog.0tutor.com/post.aspxid=122

Then you should be able to put in the music code where it says trace(ALARM!!!)


Marina says: Wednesday, April 02, 2008

How i should write code if i want after finish seconds play the music

If You can, please answer me.


Marina says: Wednesday, April 02, 2008

itChr(34)s page error. i was wrote one massege)


Marina says: Wednesday, April 02, 2008

How i should write code if i want after give a music



Admin Bob says: Tuesday, April 01, 2008

Thanks Marina, great to hear :-)

  Next 


 

 

 

 5

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact