Sunday, October 2, 2011

AS2: Timer

Source File Available Here: http://adf.ly/30cZq

Welcome to Tutorial #7! In this tutorial, I will show you a quick an easy way to make a countdown timer for your game.

Let's start by opening up a new Flash document and choose AS2 as your coding language.












Now lets create a Dynamic text box on the stage. Give it an variable name of "timer" in the properties panel.

















Lastly, we need to open up the frame's actions. So click on the frame you are on in the timeline, and hit F9 on your keyboard or right click the frame and choose "Actions". Type this:

stop();
timer = 20;
countdown = function(){
_root.timer--;
if(_root.timer<=0){
_root.timer=0;
}
}
countdownInterval = setInterval(countdown,1000);


Lets take a look at what this means:


1.) stop(); means to stop the timeline from continuing the timeline animation.
2.) timer = 20; is setting the timer text box we made to start at 20 seconds.
3.) countdown = function(){ is setting a variable name (countdown) as a function.
4.) _root.timer--; means to constantly subtract seconds from the timer.
5.) if(_root.timer<=0){ means if the timer is less than or equal to 0 seconds, then execute the following command.
6.) _root.timer=0; means to set the timer to 0, so it won't subtract into negative numbers.
7.) countdownInterval = setInterval(countdown,1000); is setting an interval of subtracting seconds as close to a normal second as possible.

Now you're done! It should look like this:









Thanks for viewing this tutorial! Don't forget to become a member of this blog to get notifications of weekly tutorials!

Source file available here: http://adf.ly/30cZq

3 comments:

  1. Good works ..
    http://www.freelancer.com/affiliates/arshidkv12/

    ReplyDelete
  2. I want both minutes and seconds in timer.what should i do for both minutes and seconds.............pls

    ReplyDelete