PDA

View Full Version : Restart timer on ZERO



Dave
November 28th, 2004, 05:14 PM
Hi Guys, i am using this code for my timer

_root.onEnterFrame = function() {
time = Math.round(getTimer()/1000);
if (time == 120) {
_root.remaining.gotoAndPlay("objectsRemaining");
}


It's being used in a game. So when they re-start the game, i want to reset the timer to start again on zero. How do i do that?

Thanks for your help.

paradox244
November 28th, 2004, 05:24 PM
try this

beginTime = Math.round(getTimer()/1000)+120;
_root.onEnterFrame = function() {
time = Math.round(getTimer()/1000);
if (time >= beginTime) {
_root.remaining.gotoAndPlay("objectsRemaining");
}

then whenever you want to restart the timer just reset the beginTime variable

Dave
November 28th, 2004, 05:59 PM
Thanks sounds good. So to reset all i have to say is beginTime =0 ?

I also have another function on another button with this script

if (_root.questionsRemaining<=0) {
delete _root.onEnterFrame;
}
}

so basically when the last item is found in this game the timer stops. Will this work with your script?

Thanks for your help.

Dave
November 28th, 2004, 06:17 PM
It didnt work!! beginTime doesn't go back to zero and neither does time.

But to reset the timer don't we need to set the variable "time" to zero? not "beginTime"?

Dave
November 28th, 2004, 06:38 PM
Actually, don't worry about it, your script did work!! Thanks for your help!! Appreciate it! :)

mdharman
November 30th, 2004, 06:40 PM
I am having the same problem. I coded my timer on a null movie clip placed offstage. When I go back to play the game again I need to reset the timer. I have tried what Paradox244 suggested with no luck. Can anyone explain what I am doing wrong?

My mc actions are:

onClipEvent (load) {
// specify end time
endTime = 8000;
}
onClipEvent (enterFrame) {
// calculate time left
timeLeft = (endTime-getTimer())/1000;
// game over
if (timeLeft<=0) {
_root.speed = 0;
_root.timeDisplay = "0";
_root.gotoAndStop("game over");
} else {
// display time left
_root.timeDisplay = timeLeft;
}
// check score
if (_root.score == 5) {
_root.gotoAndStop("game over");
}
}

I have posted the problem at http://djcc.lisd.net/2d/timer.htm.

paradox244
December 5th, 2004, 10:25 PM
getTimer does not start when you call it, it starts whenever flash is loaded. It continues to count up as long as flash is running so here is what you have to do. BTW you were close.

change the following line accordingly
endTime = 8000;
to
endTime = getTimer()+8000;

All of the rest of your code will function properly

Anyway sorry for the delay I missed the additional post.
Good Luck,
Shawn