java.lang.ObjectSimpleTimer
public class SimpleTimer
A simple timer class that allows you to keep track of how much time has passed between events. You use this class by creating a timer as a member field in your actor (or whatever):
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 Summary | |
---|---|
SimpleTimer()
|
Method Summary | |
---|---|
void |
mark()
Marks the current time. |
int |
millisElapsed()
Returns the amount of milliseconds that have elapsed since mark() was last called. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SimpleTimer()
Method Detail |
---|
public void mark()
public int millisElapsed()