[F8] Reliable Timer For Game?
I am trying to come up with a reliable timer for my game that counts how long the user has been playing a level.
I have tried setInterval but it is unreliable. Is there a better way to do it?
Ryan Thomson
why is setInterval unreliable?
I cannot get it to clear and then re-enable well.
Robot Master
What's wrong with using getTimer(); ?
EDIT: To clarify, I mean what's wrong with getting the time when a level starts, and then continuing to use getTimer(); to time it.
I tried getTimer(); but for some reason it froze after counting about 4 seconds. I will go back and try it again though.
update: it does freeze.
Code:
function timer()
{
currentTime=getTimer();
time=Math.floor((currentTime-startTime)/1000);
timeDisp.text=time;
}
I call timer(); on the enterFrame event.
Last edited by jasonpeinko; 08-18-2008 at 06:34 PM .
FK'n_dog
the code below might help
put 3 textfields in a movieclip
add a couple of buttons to the clip for testing purposes
add code to frame#1 of the movieclip -
PHP Code:
timeDisplay . text = lapDisplay . text = "00 : 00 : 00 : 000" ;
function pad ( num ){ return ( "0" + String ( num )). substr (- 2 , 2 ); }
function init (){
d = new Date (); t0 = getTimer (); method = run ;
};
function run (){
d . setTime ( getTimer ()- t0 );
var h = pad ( d . getHours ());
var m = pad ( d . getMinutes ());
var s = pad ( d . getSeconds ());
var ms = d . getMilliseconds ();
if ( ms < 100 ) ms = "0" + ms ; if ( ms < 10 ) ms = "0" + ms ;
timeDisplay . text = h + " : " + m + " : " + s + " : " + ms ;
};
function startTimer (){
method = init ; lapDisplay . text = "00 : 00 : 00 : 000" ;
};
function stopTimer (){
method = null ;
lapDisplay . text = timeDisplay . text ;
addUP ( lapDisplay . text );
timeDisplay . text = "00 : 00 : 00 : 000" ;
};
btnGo . onPress = function(){ startTimer (); };
btnStop . onPress = function(){ stopTimer (); };
this . onEnterFrame = function(){ method (); };
/////////////add up lap times/////////////////////
a = 0 ;
function addUP ( str ){
totalnum = 0 ;
this [ "arr" + a ] = new Array();
this [ "arr" + a ] = str . split ( " : " );
a ++;
for( var b = 0 ; b != a ; b ++){
h = Number ( this [ "arr" + b ][ 0 ])* 360000 ;
m = Number ( this [ "arr" + b ][ 1 ])* 60000 ;
s = Number ( this [ "arr" + b ][ 2 ])* 1000 ;
ms = parseInt ( Number ( this [ "arr" + b ][ 3 ]), 10 );
totalnum += h + m + s + ms ;
}
trace ( "total = " + totalnum + " msecs" );
trace ( time ( totalnum ));
total . text = time ( totalnum );
};
////////////////convert msecs to time///////////////////
function two ( n ){ return (( n > 9 )? "" : "0" )+ n ; };
function three ( n ){ return (( n > 99 )? "" : "0" )+(( n > 9 )? "" : "0" )+ n ; };
function time ( ms ){
var sec = Math . floor ( ms / 1000 );
ms = ms % 1000 ;
t = three ( ms );
var min = Math . floor ( sec / 60 );
sec = sec % 60 ;
t = two ( sec ) + " : " + t ;
var hr = Math . floor ( min / 60 );
min = min % 60 ;
t = two ( min ) + " : " + t ;
var day = Math . floor ( hr / 60 );
hr = hr % 60 ;
t = two ( hr ) + " : " + t ;
//t = day + " : " + t
return t
};
if you give the movieclip an instance name - mcTimer
you can call the functions from the main timeline - mcTimer.startTimer();
code prolly needs tidying up .. but it works
I went for a much simpler version of that
Code:
function pad(num){ return ("0"+String(num)).substr(-2, 2); }
function timer()
{
d.setTime(getTimer()-startTime);
var h = pad(d.getHours());
var m = pad(d.getMinutes());
var s = pad(d.getSeconds());
timeDisp.text=m+":"+s;
}
And it looked like it was working then it stops after a few seconds.
Robot Master
Do you mean the swf froze completely, or the timer just stopped? Maybe post the .fla file, hopefully someone would be able to find something.
EDIT:This works for me, hopefully it will for you...the button push is supposed to be when a level starts.
Attached Files
Last edited by the--flash; 08-19-2008 at 12:23 AM .
Thanks for all the help. I moved the code in the timer function to the enterFrame function and it worked .... .!?
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