How to add a timer to a game?

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

How to add a timer to a game?

Postby thebestdesign » Wed Nov 28, 2007 2:33 am

I'm creating a game, and i would like to add a timer in the top of the game screen, so once the game starts, the timer will run, and keep running untill the player reach the end of the game, then it will stop.
Any hints or a peace of code will be very appreciated.
thebestdesign
 
Posts: 10
Joined: Sat Nov 24, 2007 2:23 pm

Re: How to add a timer to a game?

Postby DevelopmentTeam » Wed Nov 28, 2007 3:06 am

Do this in the begining of the game
Code: Select all
int startTime = System.currentTimeMillis();



and then when the player completes the level do this

Code: Select all
int timeTakenSecs = (System.currentTimeMillis() - startTime)/1000;


now the variable timeTakenSecs will give you the number of seconds the player played the game.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: How to add a timer to a game?

Postby thebestdesign » Wed Nov 28, 2007 1:13 pm

Thanks for the answer, but what if i want to keep showing the time during the game play.
basically i want to add the time on the top of a sudoku puzzle, so once the player start playing the game, the timer will be displayed on top of the Sudoku board, and will stay there all the periode of playing (the time will keep increasing) then once the puzzle is solved completely, the time will stop in the last value.
How to do that?
Thanks.
thebestdesign
 
Posts: 10
Joined: Sat Nov 24, 2007 2:23 pm

Re: How to add a timer to a game?

Postby DevelopmentTeam » Thu Nov 29, 2007 3:27 am

you can show the timeTakenSecs variable on the screen but remember to do calculation

Code: Select all
int timeTakenSecs = (System.currentTimeMillis() - startTime)/1000;


before every time you show it.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: How to add a timer to a game?

Postby thebestdesign » Thu Nov 29, 2007 2:27 pm

Thank you ...
it works :D
thebestdesign
 
Posts: 10
Joined: Sat Nov 24, 2007 2:23 pm

Re: How to add a timer to a game?

Postby DevelopmentTeam » Fri Nov 30, 2007 3:30 am

Congratulations... happy programming... :)
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

cron