public class SimpleTimer
extends java.lang.Object
private SimpleTimer timer = new SimpleTimer();Then when you want to start the timer (for example, when a shot is fired), you call the mark() method:
timer.mark();Thereafter, you can use the millisElapsed() method to find out how long it's been since mark() was called (in milliseconds, i.e. thousandths of a second). So if you want to only allow the player to fire a shot every second, you could write:
if (timer.millisElapsed() > 1000 && Greenfoot.isKeyDown("space")) { // Code here for firing a new shot timer.mark(); // Reset the timer }
Constructor and Description |
---|
SimpleTimer() |
Modifier and Type | Method and Description |
---|---|
void |
mark()
Marks the current time.
|
int |
millisElapsed()
Returns the amount of milliseconds that have elapsed since mark()
was last called.
|
public void mark()
public int millisElapsed()