Class Timer
java.lang.Object
org.openpatch.scratch.Timer
A timer counting game time in seconds, like Scratch's timer: it stops while the game is paused
and runs faster at a higher game speed, so it agrees with everything else that happens in
ticks. Every stage and sprite has one (
getTimer()); make more as fields with
new Timer().
private Timer shooting = new Timer();
public void run() {
if (this.isKeyPressed(KeyCode.SPACE) && this.shooting.every(0.25)) {
this.shoot();
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanafter(double seconds) False for a number of seconds from the first call on, true afterwards.booleanduring(double seconds) True for a number of seconds from the first call on, false afterwards.booleanevery(double seconds) True once every number of seconds: the first time that many seconds after the first call, then again each time as many seconds have passed.doublebooleaninterval(double seconds) Alternates: true for a number of seconds, then false for as long, and so on.booleaninterval(double on, double off) Alternates: true foronseconds, then false foroffseconds, and so on.voidreset()Starts the timer again from 0, and every method below with it.
-
Constructor Details
-
Timer
public Timer()A timer starting now.
-
-
Method Details
-
reset
public void reset()Starts the timer again from 0, and every method below with it.- Scratch Block:
- reset timer
-
getSeconds
public double getSeconds()- Returns:
- the seconds of game time since the timer started or was reset
-
every
public boolean every(double seconds) True once every number of seconds: the first time that many seconds after the first call, then again each time as many seconds have passed.- Parameters:
seconds- the period- Returns:
- whether the period is over
-
during
public boolean during(double seconds) True for a number of seconds from the first call on, false afterwards.- Parameters:
seconds- how long- Returns:
- whether that time is still running
-
after
public boolean after(double seconds) False for a number of seconds from the first call on, true afterwards.- Parameters:
seconds- how long to wait- Returns:
- whether that time is over
-
interval
public boolean interval(double seconds) Alternates: true for a number of seconds, then false for as long, and so on.- Parameters:
seconds- how long each phase lasts- Returns:
- whether it is in an "on" phase
-
interval
public boolean interval(double on, double off) Alternates: true foronseconds, then false foroffseconds, and so on.- Parameters:
on- how long it is trueoff- how long it is false- Returns:
- whether it is in an "on" phase
-