Timer/stopwatch help

10 posts

Flag Post

Hey i need some help with a little thing, how do you make a stopwatch in flash, ya know it measures how long something has taken, and itd be nice to also have it stop and start at will. ya know…like a stopwatch.

 
Flag Post

I know how to make the date function but not the time lolz.

 
Flag Post

ya know on lots of game you see a timer go like on the one you see on preschool badge

 
Flag Post

There’s two ways to do it. One is frame based, and another is something I just thought of, an interval timer. I’ll just have the FPS at 12 because that’s the default, regulary I use 24 or higher.

1. Frame based

FPS = 12;
framesCounted = 0;
stopped = false;
stopper.onPress = function()
{
stopped = !stopped;
//Switch bool value
}
onEnterFrame = function()
{
if(!stopped)
{
framesCounted++;
}
timerShow.text = Math.floor(framesCounted/FPS);
}

That’s it for that.

2. Interval

FPS = 12;
seconds = 0;
stopped = false;
stopper.onPress = function()
{
stopped = !stopped;
//Switch bool value
}
IncreaseTime = function()
{
if(!stopped)
{
seconds += 1/FPS;
timertext.text = Math.floor(seconds);}
}
splitSecondInterval = setInterval(IncreaseTime,1000/FPS);

I think that’d work.

 
Flag Post

Both intervals and frame events can be very inaccurate when it comes to time measurement. Best thing to do is use the Date() object or getTimer() to measure how much time has passed.

 
Flag Post

ya arcane i know hat you mean cuz there can be slowdowns ingame but no much stuff goes on in the game im using this in(mouse avoider)

 
Flag Post

use this:

class Timer {

private var timeStarted:Number = 0;
private var timeStoped:Number = 0;
private var isRunning:Boolean = false;

function Timer() {
trace(“init”)
}
private function start(startTime:Number):Void {
timeStarted = startTime;
isRunning = true;
}

public function stop():Void {
if(!isRunning) {
var date:Date = new Date();
timeStoped = date.getTime();
delete(date);
isRunning = false;
}
}

public function resume():Void {
if(!isRunning) {
var date:Date = new Date();
start(timeStarted + (date.getTime() – timeStoped));
delete(date);
}
}

public function restart():Void {
var date:Date = new Date();
start(date.getTime());
delete(date);
}

public function getMilliseconds():Number {
if(isRunning) {
var date:Date = new Date();
return(date.getTime() – timeStarted);
delete(date);
} else {
return(timeStoped – timeStarted)
}
}

}

 
Flag Post

Both intervals and frame events can be very inaccurate when it comes to time measurement.

However, if the time you want to measure is a countdown, you would sort of want it to slow down with the game, because it would be unfair if a player on low quality or with a faster computer could do more “playing” in the time slot than someone for whom the game slowed down.

 
Flag Post

However, if the time you want to measure is a countdown, you would sort of want it to slow down with the game, because it would be unfair if a player on low quality or with a faster computer could do more “playing” in the time slot than someone for whom the game slowed down.

Four Second Firestorm in real-time FTL.

 
Flag Post

However, if the time you want to measure is a countdown, you would sort of want it to slow down with the game, because it would be unfair if a player on low quality or with a faster computer could do more “playing” in the time slot than someone for whom the game slowed down.


Not always, particularly if there are scoreboards or rewards, as it allows an uneven playfield and a means to cheat. Its almost always better to go with an accurate timer (though understandable there are exceptions), if indeed it is supposed to be time measurement and this thread is indeed about time measurement.