|
-
[CS3] Timer help required!
Hi all,
I've made a countdown timer for my game, once it reaches zero it goes to the next frame, thats all fine but my problem is this:
if you complete the level before the time is up, how can i stop the timer and display the remaining time in the following frame. Also it would be nice if i could take the remaining time, multiply it by 10 and display that as a final score.
This is the code i have for the time so you can see my method.
Code:
timer=120;
countdown=function(){
timer--;
if (timer==0){
clearInterval(doCountdown);
_parent.nextFrame();
}
}
doCountdown=setInterval(countdown,1000);
Thanks for any help
Adam
-
1st use functions- they solve many design errors,
that beeing said I would group your code in a function and capture the time when you start the interval and use that timeStamp to compute the time that elapsed starting from then.
PHP Code:
function startTimer(){
_root.startTime = getTimer();
timer=120;
countdown=function(){
timer--;
if (timer==0){
clearInterval(_root.doCountdown);
_parent.nextFrame();
}
}
_root.doCountdown=setInterval(countdown,1000);
}
function stopTimer(){
clearInterval(_root.doCountdown);
var timeDifference = (getTimer() - _root.startTime)/1000;//in seconds
trace("score: "+int(timeDifference*10)+" pts");
_parent.nextFrame();
}
-
I don't fully understand the code i'm afraid,
I tried putting that code into my game but the following happened: the dynamic text box within the game level just displays "_level0.instance39.timer" and in the following results screen it displays "0"
-
the first function did what you showed us here before (I assume you understood that code at least), except that it stores the current flash running time in a variable called _root.startTime
the 2nd function (should be called when interrupting ) interrupts the interval,- calculates the time difference and traces it.
I havent tested the code in flash so changes might be that you need to debug it but it looks ok to me.
-
it works fine in flash for me
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|