Be a Supporter!

AS: Timer

  • 3,961 Views
  • 23 Replies
New Topic Respond to this Topic
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
AS: Timer 2005-11-14 11:39:26 Reply

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
Khao
Khao
  • Member since: Sep. 20, 2003
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Timer 2005-11-14 11:42:29 Reply

Good job :)

Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Timer 2005-11-14 11:43:08 Reply

Yeah, nice tutorial there


BBS Signature
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: Timer 2005-11-14 11:43:10 Reply

niiiice simple and useful.

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Timer 2005-11-14 11:46:10 Reply

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
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: Timer 2005-11-14 11:47:40 Reply

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

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Timer 2005-11-14 11:51:42 Reply

_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
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Timer 2005-11-14 12:08:09 Reply

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
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Timer 2005-11-14 13:06:50 Reply

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
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: Timer 2005-11-14 13:08:46 Reply

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
Gbanger
Gbanger
  • Member since: Jan. 29, 2006
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS: Timer 2006-02-03 23:46:01 Reply

**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)

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS: Timer 2006-02-03 23:49:35 Reply

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
a compessor wil raise the volume while lowering the db - chronamut

Gbanger
Gbanger
  • Member since: Jan. 29, 2006
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS: Timer 2006-02-04 00:07:23 Reply

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

Gbanger
Gbanger
  • Member since: Jan. 29, 2006
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS: Timer 2006-02-04 00:24:38 Reply

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?

Garden-Stater
Garden-Stater
  • Member since: Mar. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Timer 2006-02-10 20:51:43 Reply

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. :)

Garden-Stater
Garden-Stater
  • Member since: Mar. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Timer 2006-02-10 20:53:56 Reply

I should mention I'm using Flash MX 2004.

pmouth
pmouth
  • Member since: Sep. 30, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Timer 2006-02-11 00:32:53 Reply

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

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Timer 2006-02-11 04:52:18 Reply

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
motmot
motmot
  • Member since: Feb. 3, 2006
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Timer 2006-02-23 14:25:03 Reply

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

deslona
deslona
  • Member since: Jul. 1, 2004
  • Offline.
Forum Stats
Member
Level 34
Blank Slate
Response to AS: Timer 2007-06-20 20:41:20 Reply

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

chad-duncan
chad-duncan
  • Member since: Dec. 16, 2006
  • Offline.
Forum Stats
Member
Level 14
Game Developer
Response to AS: Timer 2007-07-03 11:46:34 Reply

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.

No-Brainer
No-Brainer
  • Member since: May. 26, 2007
  • Offline.
Forum Stats
Member
Level 08
Animator
Response to AS: Timer 2008-11-01 21:00:48 Reply

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?


tfw no face

BBS Signature
BillysProgrammer
BillysProgrammer
  • Member since: Sep. 17, 2008
  • Offline.
Forum Stats
Member
Level 16
Gamer
Response to AS: Timer 2008-11-01 21:34:38 Reply

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

imdead597
imdead597
  • Member since: Jan. 24, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS: Timer 2009-08-09 12:26:48 Reply

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.