00:00
00:00
Newgrounds Background Image Theme

Chan99 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Timer 2005-11-14 11:39:26


AS: Main

SAMPLE

Introduction
In this tutorial we are going to make a timer that starts at 00 hours 00 minutes 00 seconds and 00 milliseconds.
We'll make a button for stop and start the timer.
I hope this will go to AS: Main under Specific Effects

The Play/Stop buttons
First make three buttons:
Play - The play button
Pause - The pause button
Reset - The reset button

Don't put any AS in the buttons since the AS will go only in the frame.
For changing the instance names on the play button we'll have to Press CTRL+F3 for the properties bar and there is a little text box which says "instance name".
In that box write instead "playbutton" without quotes of course.
Do the same to the pause and reset buttons but instead in pause button the instance is "pausebutton" and on the reset button the instance name is "reset".

The Text Boxes
This is really simple. Just make a text box. Press CTRL+F3 for the properties and change to dynamic text box instead of static text. Copy/paste the dynamic text box 3 times so you got 4 dynamic text boxes.
Now we will show the variables we'll use in these dynamic text boxes.
Name the first text box "hours". To do this you can't name the text box in instance name as before because then the text box will go nuts. No, instead name it "hours" in the box where it says Var:
Name the second text box (in the same way): "minutes", the third "seconds" and the fourth "milli".
Now we got our text boxes for our timer.

Now for the AS (ActionScript)
Below I will post the only AS you'll need and comment the lines with bold //'s.
Place this AS in the frames AS by pressing F9.
Here is the AS:

_root.pausebutton._visible = 0;
// This will make the pausebutton being unvisible when can't use
function restartTimer() {
hours = "00";
minutes = "00";
seconds = "00";
milli = "00";
buttonPressTime = getTimer()/1000-pauseLength;
pause();
}
// This makes a function where the variables becomes 0. Used for the resetbutton
function pause() {
pauseTime = getTimer()/1000;
timing = false;
}
// This makes a function that makes the variable timing false making the timer to pause
function unpause() {
unpauseTime = getTimer()/1000;
pauseLength = (unpauseTime-pauseTime)+pauseLength;
timing = true;
}
/*This makes a function making the timing true and checking what the timer was when paused. Used for the play button. */
_root.onEnterFrame = function() {
totalTime = getTimer()/1000-pauseLength;
goTime = totalTime-buttonPressTime;
//Makes a onClipEvent(enterFrame) function
if (timing) {
hours = Math.floor(goTime/3600);
minutes = Math.floor((goTime/3600-hours)*60);
seconds = Math.floor(((goTime/3600-hours)*60-minutes
)*60);
milli = Math.floor((gotime-(seconds+(minutes*60)+(
hours*3600)))*100);
if (seconds<10) {
seconds = "0"+seconds;
}
if (minutes<10) {
minutes = "0"+minutes;
}
if (hours<10) {
hours = "0"+hours;
}
if (milli<10) {
milli = "0"+milli;
}
}
};
/* This function basicly do so the timer will actually go up and makes the minute go up when the second timer is 60 etc */
playbutton.onRelease=function() {
unpause();
pausebutton._visible = 1;
playbutton._visible = 0;
}
/* Makes the timer go up when the play button is pressed. Also makes the pause button visible and play button invisible. Uses the function unpause */
pausebutton.onRelease=function() {
pause();
playbutton._visible = 1;
pausebutton._visible = 0;
}
//The opposite of the playbutton with the pause button
reset.onRelease= function() {
restartTimer();
pausebutton._visible = 0;
playbutton._visible = 1;
}
/* Makes the pause button invisble and the play button visible. Also uses the function restartTimer*/

//guywithhiscomp


BBS Signature

Response to AS: Timer 2005-11-14 11:43:08


Yeah, nice tutorial there


BBS Signature

Response to AS: Timer 2005-11-14 11:43:10


niiiice simple and useful.

Response to AS: Timer 2005-11-14 11:46:10


At 11/14/05 11:42 AM, -KhAo- wrote: Good job :)

Thanks :) I'm pretty new to AS so I like good rewievs.

And, some of the bold //'s are going below the line so it will not work correctly if you just copy/paste the code. Either delete all messages in the //'s or copy/paste this new correct code without the //'s:
_root.pausebutton._visible = 0;
function restartTimer() {
hours = "00";
minutes = "00";
seconds = "00";
milli = "00";
buttonPressTime = getTimer()/1000-pauseLength;
pause();
}
function pause() {
pauseTime = getTimer()/1000;
timing = false;
}
function unpause() {
unpauseTime = getTimer()/1000;
pauseLength = (unpauseTime-pauseTime)+pauseLength;
timing = true;
}
_root.onEnterFrame = function() {
totalTime = getTimer()/1000-pauseLength;
goTime = totalTime-buttonPressTime;
if (timing) {
hours = Math.floor(goTime/3600);
minutes = Math.floor((goTime/3600-hours)*60);
seconds = Math.floor(((goTime/3600-hours)*60-minutes
)*60);
milli = Math.floor((gotime-(seconds+(minutes*60)+(
hours*3600)))*100);
if (seconds<10) {
seconds = "0"+seconds;
}
if (minutes<10) {
minutes = "0"+minutes;
}
if (hours<10) {
hours = "0"+hours;
}
if (milli<10) {
milli = "0"+milli;
}
}
};
playbutton.onRelease=function() {
unpause();
pausebutton._visible = 1;
playbutton._visible = 0;
}
pausebutton.onRelease=function() {
pause();
playbutton._visible = 1;
pausebutton._visible = 0;
}
reset.onRelease= function() {
restartTimer();
pausebutton._visible = 0;
playbutton._visible = 1;
}


BBS Signature

Response to AS: Timer 2005-11-14 11:47:40


Im pretty sure Actioscript works with commentaries (aka //'s). So i dont see any need of taking them off.

Response to AS: Timer 2005-11-14 11:51:42


_root.pausebutton._visible = 0;
function restartTimer() {
hours = minutes = seconds = milli = "00";
buttonPressTime = getTimer()/1000-pauseLength;
pause();
}
function pause() {
pauseTime = getTimer()/1000;
timing = false;
}
function unpause() {
unpauseTime = getTimer()/1000;
pauseLength = (unpauseTime-pauseTime)+pauseLength;
timing = true;
}
_root.onEnterFrame = function() {
totalTime = getTimer()/1000-pauseLength;
goTime = totalTime-buttonPressTime;
if (timing) {
textBox = Math.floor(goTime/3600)+":"+Math.floor((go
Time/3600-hours)*60)+":"+Math.floor(((goTi
me/3600-hours)*60-minutes)*60)+":"+Math.fl
oor((gotime-(seconds+(minutes*60)+(hours*3
600)))*100);
seconds<10 ? seconds= "0"+seconds: 0
minutes<10 ? minutes = "0"+minutes : 0
hours<10 ? hours = "0"+hours : 0
milli<10? milli = "0"+milli : 0
};
playbutton.onRelease=function() {
unpause();
pausebutton._visible = 1;
playbutton._visible = 0;
}
pausebutton.onRelease=function() {
pause();
playbutton._visible = 1;
pausebutton._visible = 0;
}
reset.onRelease= function() {
restartTimer();
pausebutton._visible = 0;
playbutton._visible = 1;
}

Just modified a bit, only need one textbox with the var "textBox" attached.


Sup, bitches :)

BBS Signature

Response to AS: Timer 2005-11-14 12:08:09


At 11/14/05 11:47 AM, Darkfire_Blaze wrote: Im pretty sure Actioscript works with commentaries (aka //'s). So i dont see any need of taking them off.

Well. it's because the commentaries went below the line.
It doesn't mather with /* and */ though but if a // commentary goes to the line below the text that goes to the line below will appear as error.


BBS Signature

Response to AS: Timer 2005-11-14 13:06:50


At 11/14/05 11:39 AM, guywithhiscomp wrote: In this tutorial we are going to make a timer that starts at 00 hours 00 minutes 00 seconds and 00 milliseconds.

you know, you are off to a roaring start. great job, kid.
now, start helping around the flash forum. we <3 you and your secksay tutorials.


BBS Signature

Response to AS: Timer 2005-11-14 13:08:46


At 11/14/05 12:08 PM, guywithhiscomp wrote: Well. it's because the commentaries went below the line.
It doesn't mather with /* and */ though but if a // commentary goes to the line below the text that goes to the line below will appear as error.

oh yeah. I really didnt notice that.

And... I think authorblues is asking for buttsecks. Run kid, run.

XD kidin

Response to AS: Timer 2006-02-03 23:46:01


**Error** Scene=Scene 1, layer=Layer 4, frame=1:Line 21: ')' expected
Time/3600-hours)*60)+":"+Math.floor(((goTi

**Error** Scene=Scene 1, layer=Layer 4, frame=1:Line 22: ')' expected
me/3600-hours)*60-minutes)*60)+":"+Math.fl

**Error** Scene=Scene 1, layer=Layer 4, frame=1:Line 24: ')' expected
600)))*100);

Total ActionScript Errors: 3 Reported Errors: 3

i get those errors with just that one code + 1 texbox with var - "textBox".. what the hell am i doing wrong.. (actionscript was just on the first scene (its only one scene)

Response to AS: Timer 2006-02-03 23:49:35


i get those errors with just that one code + 1 texbox with var - "textBox".. what the hell am i doing wrong.. (actionscript was just on the first scene (its only one scene)

Are you on Flash 8?This code might have been made in Flash 8 and can't be used in other Flash versions.


wat

Response to AS: Timer 2006-02-04 00:07:23


yep im in flash 8 cant figure out for the life of me why i cant get it to work

Response to AS: Timer 2006-02-04 00:24:38


Ok even with just using the top code ( well the one he fixed) when u push play the text boxes display "NaN".... if u hit reset they display 00..... am i supposed to have numbers in there to start? and im right in putting the "minutes" into where it says var and not instance yeah?

Response to AS: Timer 2006-02-10 20:51:43


I'm having similar problems. When I used the original code, and filled the text boxes with 0s, the words "NaNaNaNa" came up in place of the counter. When I used the original code without zeros in the text boxes brackets came up instead of numbers. When I used the the last modification of the code an error message came up similar to the above poster.

Nevertheless, I still think this code is really cool. It's exactly what I need for my current project. If you have any suggestions about how to get it working, I'd appreciate it. :)

Response to AS: Timer 2006-02-10 20:53:56


I should mention I'm using Flash MX 2004.

Response to AS: Timer 2006-02-11 00:32:53


i think Na means the text box is too small for the variable that fits in it, so make the boxes bigger

Response to AS: Timer 2006-02-11 04:52:18


At 2/11/06 12:32 AM, pmouth wrote: i think Na means the text box is too small for the variable that fits in it, so make the boxes bigger

Hehe, that's wrong actually. It's NaN, and it means "Not a number". Personally I would solve it by using two separate variables for each time unit (for example displayHours and hours), to keep strings and numbers separate.


BBS Signature

Response to AS: Timer 2006-02-23 14:25:03


wow your great can you teach me more, about life damaging, thx for that timer

Response to AS: Timer 2007-06-20 20:41:20


Can someone please post a solution to this problem for Flash 8?

Response to AS: Timer 2007-07-03 11:46:34


I had the same problems some other people also had, but I changed a couple of things to make it work for me. Here is the code I have:

_root.pausebutton._visible = 0;
function restartTimer() {
hours = "00";
minutes = "00";
seconds = "00";
milli = "00";
buttonPressTime = getTimer()/1000-pauseLength;
pause();
}
function pause() {
pauseTime = getTimer()/1000;
timing = false;
}
function unpause() {
unpauseTime = getTimer()/1000;
pauseLength = (unpauseTime-pauseTime)+pauseLength;
timing = true;
}
_root.onEnterFrame = function() {
if (pauseTime == undefined) {
pauseTime = 0;
}
if (pauseLength == undefined) {
pauseLength = 0;
}
if (buttonPressTime == undefined) {
buttonPressTime = getTimer()/1000-pauseLength;
}
totalTime = getTimer()/1000-pauseLength;
goTime = totalTime-buttonPressTime;
if (timing) {
hours = Math.floor(goTime/3600);
minutes = Math.floor((goTime/3600-hours)*60);
seconds = Math.floor(((goTime/3600-hours)*60-minutes)*6 0);
milli = Math.floor((goTime-(seconds+(minutes*60)+(hou rs*3600)))*100);
if (seconds<10) {
seconds = "0"+seconds;
}
if (minutes<10) {
minutes = "0"+minutes;
}
if (hours<10) {
hours = "0"+hours;
}
if (milli<10) {
milli = "0"+milli;
}
}
};
playbutton.onRelease = function() {
unpause();
pausebutton._visible = 1;
playbutton._visible = 0;
};
pausebutton.onRelease = function() {
pause();
playbutton._visible = 1;
pausebutton._visible = 0;
};
reset.onRelease = function() {
restartTimer();
pausebutton._visible = 0;
playbutton._visible = 1;
};

Note: If you just copy and paste this code, it may have syntax errors. Once you paste it in there, check to see if some of the code got pushed to another line.

Response to AS: Timer 2008-11-01 21:00:48


how would you make a countdown timer? i want it to start on 5:00 and go to another frame when it hits 0:00, someone help me?

Response to AS: Timer 2008-11-01 21:34:38


Lookin pretty good GuyWithHisComp, tested it out on both Flash 8, CS3, and CS4, all seems to work for me.

Aswell, comment lines dont matter

Response to AS: Timer 2009-08-09 12:26:48


how would you make a delay timer? im trying to make a timer that would cause a half second delay before your character can move again.