Your Ad Here

December 19, 2010

Timer in Flash

Reactions: 

Category: Advance
Actionscript: 2.0

  In this tutorial you will learn how to create a Timer In Adobe Flash. Timer is very useful for time delayed functions. It is also used in Flash Games.


1- Open up Macromedia Flash and create a new file.
2- Create a ‘Dynamic text field’ (In the Properties select Dynamic Text). Write in the text field properties in the ‘instance name’ as ‘txt_timer’.
3- Now create a new layer and select the first frame of the created layer.
3- While the frame is still selected open up the Actions Panel (F9) and write the actions as follows:

timer = 10;
txt_timer.text = timer;
count = function () {
                timer--;
                txt_timer.text = timer;
                if (timer == 0) {
                                txt_timer.text = "Timer Ended";
                                clearInterval(countInterval);
                }
};
countInterval = setInterval(count, 1000);

4- All done. Hit Ctrl+Enter to check the movie.

Description:
//This will set the time period of the timer in seconds
 timer = 10;
//This will output the timer on the text field you have created
txt_timer.text = timer;
//This is the main count function which will decrease the timer count
//This will be triggered after every 1000 milliseconds i.e. after 1 second as we have set this time in the setInterval function
count = function () {
                timer--;
                txt_timer.text = timer;
                if (timer == 0) {
                                txt_timer.text = "Timer Ended";
                                clearInterval(countInterval);
                }
};
countInterval = setInterval(count, 1000);


2 comments:

  1. how a function can be called when timer finishes ?

    ReplyDelete
  2. You can call the function in the if loop here:
    if (timer == 0) {
    functiontocall(); //call function here
    txt_timer.text="TimerEnded"; clearInterval(countInterval);
    }

    ReplyDelete

Please use sensible language.....!