Package org.xjge.core
Class Timer
java.lang.Object
org.xjge.core.Timer
public class Timer
extends java.lang.Object
A simple monotonic timing mechanism. Useful for game events and other
systems that require specialized timing intervals.
- Since:
- 2.0.0
-
Constructor Summary
Constructors Constructor Description Timer(int time, int speed)
Creates a new timer object that will step forward every time the specified number of update cycles have passed.Timer(int time, int speed, java.beans.PropertyChangeListener observer)
Creates a new timer object that will step forward every time the specified number of update cycles have passed.Timer(int time, int speed, java.util.List<java.beans.PropertyChangeListener> observers)
Creates a new timer object that will step forward every time the specified number of update cycles have passed. -
Method Summary
Modifier and Type Method Description int
getTime()
Obtains the current value of the time field.void
reset()
Resets the time of the timer to its initial duration.void
restart()
Sets the timer back to its initial state and starts ticking again.void
start()
Starts the timer.void
stop()
Stops the timer.void
update()
Updates the timer.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
Timer
public Timer(int time, int speed)Creates a new timer object that will step forward every time the specified number of update cycles have passed.- Parameters:
time
- the total number of intervals the timer must complete before it is finishedspeed
- the number of game ticks to wait before stepping forward. A single tick typically takes 16 milliseconds.- See Also:
Game.tick(int)
-
Timer
public Timer(int time, int speed, java.beans.PropertyChangeListener observer)Creates a new timer object that will step forward every time the specified number of update cycles have passed. This constructor will notify an observer once the timer has finished.- Parameters:
time
- the total number of steps the timer must complete before it is finishedspeed
- the number of update ticks to wait before stepping forward. A single tick typically takes a millisecond.observer
- the object waiting for this timer to finish- See Also:
Observable
,Game.tick(int)
-
Timer
public Timer(int time, int speed, java.util.List<java.beans.PropertyChangeListener> observers)Creates a new timer object that will step forward every time the specified number of update cycles have passed. This constructor will notify multiple observers once the timer has finished.- Parameters:
time
- the total number of steps the timer must complete before it is finishedspeed
- the number of update ticks to wait before stepping forward. A single tick typically takes a millisecond.observers
- the objects waiting for this timer to finish- See Also:
Observable
,Game.tick(int)
-
-
Method Details
-
start
public void start()Starts the timer. -
stop
public void stop()Stops the timer. Doing so will pause it at its current time. -
reset
public void reset()Resets the time of the timer to its initial duration. -
update
public void update()Updates the timer. It is expected that implementing objects using a timer component will supply some sort ofupdate()
method as part of a larger logic loop through which this can be called. -
restart
public void restart()Sets the timer back to its initial state and starts ticking again. Not to be confused withreset()
which will only effect the timers time value. Restarting a timer will notify its observers once it has finished even if it had finished previously. -
getTime
public int getTime()Obtains the current value of the time field.- Returns:
- the current time the timer is at
-