# Thread: Timed object generation/ for guitar hero game

1. Member
Join Date
Jan 2013
Location
'Murica
Posts
25
Rep Power
0

## Timed object generation/ for guitar hero game

Hello and thank you for your patience, time, effort, and energy.

I want to generate a new tile every 1,5,10,15 (please check the code to gain an understanding of where I'm going) the session last for 4m :20 sec, I need help using the date package, I tried using the sleep method however I don't want to pause the whole thread, so I need help setting up the time aspect of the key generation (assume all other code works).

Thank you so much you! Drink Coffee happy coding.

Java Code:
```mport info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.*;
import java.util.*;
//importations, just in case you were wondering

//locations and actor world are set up
//assume all other code works
long start = System.currentTimeMillis( );
long end = System.currentTimeMillis( );
long diff = end - start;
while(diff<260)// 4*60 + 20
{

if(diff%1==0)
if(diff%5==0)
if(diff%10==0)
if(diff%150==0)
world.show();

end = System.currentTimeMillis( );
diff = end - start;

}```
I'm using grid world to set up the locations, the tiles are actors that move south every turn
Last edited by 765891; 05-22-2014 at 05:23 AM.

2. Senior Member
Join Date
Feb 2014
Posts
447
Rep Power
4

## Re: Timed object generation/ for guitar hero game

Hmm. I am a little confused: you get the time in milliseconds not seconds!
So if you want to do something every second then you should check the seconds part.

And then you can add a small sleep, too. If you know how many miliseconds your code took, you can quickly calculate a small sleep duration. (Or just add a small sleep)

And you must remember the second you did something. Else you do something multiple times.

3. ## Re: Timed object generation/ for guitar hero game

Have a look at the java.util.Timer class; it can fire TimerTasks at regular intervals.

kind regards,

Jos

4. Member
Join Date
Jan 2013
Location
'Murica
Posts
25
Rep Power
0

## Re: Timed object generation/ for guitar hero game

Jackpot! THANK YOU! Didn't realize the flaw in my code.

Originally Posted by kneitzel
Hmm. I am a little confused: you get the time in milliseconds not seconds!
So if you want to do something every second then you should check the seconds part.

And then you can add a small sleep, too. If you know how many miliseconds your code took, you can quickly calculate a small sleep duration. (Or just add a small sleep)

And you must remember the second you did something. Else you do something multiple times.

5. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Timed object generation/ for guitar hero game

Good that you know what the problem is, that's a learning moment. But don't ignore Jos's suggestion either; why reinvent the wheel when you can just use existing standard APIs.