Game Development Community

Timer Trouble

by Georgina Benedetti · in Torque Game Builder · 03/02/2011 (4:48 pm) · 2 replies

I have made a timer that ticks down 0.1, but it likes to have multiple decimals on the end when I only want two decimal figures. Is there a way to constrict this?

Also my timer likes to keep going down past zero when I want it to stop when it hits zero. I understand why it is doing it but I'm not sure how I should change about my code.

<code>

//TIMER

function timeClass::onLevelLoaded(%this, %scenegraph)
{
%this.schedule(3000, "timer");
$time = 5.00;
}

function timeClass::timer(%this)
{
$time -= 0.01;
clock.text = "Time:" SPC $time;

if($time > 0.00)
{
%this.schedule(125, "timer");
}
}

<code>

#1
03/02/2011 (7:51 pm)
$time = mFloatLength($time , 2);
#2
03/03/2011 (2:52 am)
To maintain precision you may want to assign truncated timer value to some other variable and not back to itself.
clock.text = "Time:" SPC mFloatLength($time , 2);

And if you want the timer to stop above zero, compare it to step value rather than zero.
function timeClass::onLevelLoaded(%this, %scenegraph)
{
    ...
    %this.step = 0.01;
    ...
}

...

function timeClass::timer(%this)
{
    $time -= %this.step;
    ...
    if($time >= %this.step) {
        // still enough time for another step
        %this.schedule(125, "timer");
    }
    else {
        // time runned out
        $time = 0.0;
        clock.text = "Time: 0.00";
    }
}