Class Timer

java.lang.Object
org.openpatch.scratch.Timer

public class Timer extends Object
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
    Constructor
    Description
    A timer starting now.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    after(double seconds)
    False for a number of seconds from the first call on, true afterwards.
    boolean
    during(double seconds)
    True for a number of seconds from the first call on, false afterwards.
    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.
    double
     
    boolean
    interval(double seconds)
    Alternates: true for a number of seconds, then false for as long, and so on.
    boolean
    interval(double on, double off)
    Alternates: true for on seconds, then false for off seconds, and so on.
    void
    Starts the timer again from 0, and every method below with it.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 for on seconds, then false for off seconds, and so on.
      Parameters:
      on - how long it is true
      off - how long it is false
      Returns:
      whether it is in an "on" phase