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

Hi!

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.

timebola.cpp

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

Time::Time()
{

lastTime=500;

}

//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;
//}

Time::~Time(){}

timebola.h

#ifndef TIMEBOLA_H
#define TIMEBOLA_H

class Time
{
private:
int startTime;
int curTime;
int timer;
int lastTime;
//bool pauseTime;

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

Time();
~Time();
};
#endif

Thanks

Susana

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() {  
	if(playing)  
		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() {  
	if(playing)  
		timer.setCurrentTime(ofGetElapsedTimef());  
}  

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.
Susana