how to program to stop a timer when the game is over?


I’m learning C++ and oF (just had 4 classes) and I’m developing a very simple game, in which a ball has to reach the top of the screen (the ball moves with sound, using OfxOsc through Max/MSP and Osculator) and when it reaches the top, the game is over.
I have a timer that counts miliseconds ( I don’t know how to put it counting seconds :stuck_out_tongue: ) that is resetted everytime the user clicks the key ‘s’.
When the ball reaches the top, appears an image saying “Game over”, but the timer should stop when the game is over and I really don’t know how to do it. (because it will win the user that takes less time to reach the top, so the timer has to stop).
Can anyone help me? I tried so many things… I erased almost all, but put in comments the last ones I tried. I don’t know how to program something to stop the time.
I’m using oF 007 with Xcode.


#include “ofMain.h”
#include “timebola.h”




//faz reset de todos os valores de tempo
void Time:: setStartTime(int val)
startTime=ofGetElapsedTimeMillis()/1000;//define o novo tempo para o temporizador
lastTime = val;//define o valor do temporizador
//pauseTime = false;


void Time::setCurrentTime(int t)
// usa o tempo actual do sistema desde que o jogo começa
curTime= (ofGetElapsedTimeMillis()/1000) - startTime;
//pauseTime = false;
timer = t + curTime;

int Time::getCurrentTime()
return timer;
//pauseTime = false;

//int Time::stopCurrentTime()
//pauseTime = true;



#ifndef TIMEBOLA_H
#define TIMEBOLA_H

class Time
int startTime;
int curTime;
int timer;
int lastTime;
//bool pauseTime;

void setStartTime(int);
void setCurrentTime(int);
int getCurrentTime();
//int stopCurrentTime();




To get time in seconds use ofGetElapsedTimef()

There are a few ways to do this that range from rather complicated to fairly simple. One way that’s really easy would be to have a variable that’s the current time of your game that you update in the update() method only when the game is running, like so:

void update() {  
		gameTime = ofGetElapsedTimef(); // assumes game time is a float & represents seconds  

You can keep track of your game state that way. You could also use the timer class like you’re doing and just not update it when the user isn’t playing:

void update() {  

Hi Joshua!
Thanks for your help.
I re-wrote the class and put it simpler and now it’s working… :slight_smile:
I tried ofGetEllapsedTimef(), but the time isn’t changing. Well, but it isn’t serious in this case.