Kiwi.Time.Clock Class
The Clock class offers a way of tracking time within a game. When creating a new Clock you should NOT directly instantiate this class but instead use the addClock method on a ClockManager.
- The MasterClock is a property of the Kiwi.Time.Manager class and tracks real world time in milliseconds elapsed since the application started. This happens automatically and there is no need to do anything to set this up.
- An instance of a clock is used to track time in arbitrary units (milliseconds by default)
- A clock can be started, paused, unpaused and stopped. Once stopped, re-starting the clock again will reset it.
- Any number of timers can be attached to a clock. See the Kiwi.Time.Timer class for timer details.
- If the clock is paused, any timers attached to the clock will take this into account and not continue to fire events until the clock is unpaused. (Note that this is not the same as pausing timers, which can be done manually and needs to be undone manually.)
Constructor
Kiwi.Time.Clock
-
manager
-
master
-
name
-
[units=1000]
Parameters:
-
manager
ClockManagerThe ClockManager that this clock belongs to. .
-
master
Kiwi.Time.MasterClockThe MasterClock that it is getting the time in relation to.
-
name
StringThe name of the clock.
-
[units=1000]
Number optionalThe units that this clock is to operate in.
Returns:
This Clock object.
Item Index
Methods
Methods
addTimer
-
timer
Add an existing Timer to the Clock.
Parameters:
-
timer
TimerThe Timer object instance to be added to ths Clock.
Returns:
This Clock object.
checkExists
-
name
Check if the Timer already exists on this Clock
Parameters:
-
name
StringThe name of the Timer.
Returns:
true if the Timer exists, false if not.
createTimer
-
name
-
[delay=1]
-
[repeatCount=0]
-
[start=true]
Creates a new Timer and adds it to this Clock.
Parameters:
-
name
StringThe name of the Timer (must be unique on this Clock).
-
[delay=1]
Number optionalThe number of clock units to wait between firing events (default 1)
-
[repeatCount=0]
Number optionalThe number of times to repeat this Timer (default 0)
-
[start=true]
Boolean optionalIf the timer should start.
Returns:
The newly created Timer.
elapsed
()
Number
public
The number of clock units elapsed since the clock was most recently started (not including time spent paused)
Returns:
number of clock units.
elapsedSinceFirstStarted.
()
Number
public
The number of clock units elapsed since the clock was first started.
Returns:
number of clock units.
elapsedSinceLastPaused.
()
Number
public
The number of clock units elapsed since the clock was most recently paused.
Returns:
number of clock units.
elapsedSinceLastStopped.
()
Number
public
The number of clock units elapsed since the clock was most recently stopped.
Returns:
number of clock units.
elapsedSinceLastUnpaused.
()
Number
public
The number of clock units elapsed since the clock was most recently unpaused.
Returns:
number of clock units.
isPaused
()
Boolean
public
Check if the clock is in the paused state.
Returns:
true if paused.
isRunning
()
Boolean
public
Check if the clock is currently running.
Returns:
true if running.
isStopped
()
Boolean
public
Check if the clock is in the stopped state.
Returns:
true if stopped.
objType
()
String
public
The type of object that this is.
Returns:
pause
()
Kiwi.Time.Clock
public
Pause the clock. The clock can only be paused if it is already running.
Returns:
This Clock object.
removeTimer
-
[timer=null]
-
[timerName='']
Remove a Timer from this Clock based on either the Timer object or its name.
Parameters:
-
[timer=null]
Timer optionalThe Timer object you wish to remove. If you wish to delete by Timer Name set this to null.
-
[timerName='']
String optionalThe name of the Timer object to remove.
Returns:
True if the Timer was successfully removed, false if not.
resume
()
Kiwi.Time.Clock
public
Resume the clock. The clock can only be resumed if it is already paused.
Returns:
This Clock object.
start
()
Clock
public
Start the clock. This resets the clock and starts it running.
Returns:
This Clock object.
started
()
Number
public
Get the most recent time the clock was started relative to the master clock.
Returns:
milliseconds.
stop
()
Kiwi.Time.Clock
public
Stop the clock. Clock can only be stopped if it is already running or is paused.
Returns:
This Clock object.
stopAllTimers
()
Clock
public
Stop all timers attached to the clock.
Returns:
This Clock object.
toMilliseconds.
()
Number
public
Convert a number to milliseconds based on clock units.
Returns:
milliseconds.
toString
()
String
public
Returns a string representation of this object.
Returns:
a string representation of the instance.
update
()
public
Updates all Timers linked to this Clock.
Properties
_elapsedState
Number
private
An internal reference to the state of the elapsed timer
Default: 0
_isPaused
Boolean
private
Whether the clock is in a paused state.
Default: false
_isRunning
Boolean
private
Whether the clock is in a running state.
Default: false
_isStopped
Boolean
private
Whether the clock is in a stopped state.
Default: true
_timeFirstStarted
Number
private
The time the clock was first started relative to the master clock.
Default: null
_timeLastPaused
Number
private
The time the clock was most receently paused relative to the master clock.
Default: null
_timeLastStarted
Number
private
The time the clock was most recently started relative to the master clock.
Default: null
_timeLastStopped
Number
private
The time the clock was most recently stopped relative to the master clock.
Default: null
_timeLastUnpaused
Number
private
The time the clock was most recently unpaused relative to the master clock.
Default: null
_totalPaused
Number
private
The total number of milliseconds the clock has been paused since it was last started.
Default: 0
manager
ClockManager
public
The time manager that this clock belongs to.
name
String
public
Name of the clock
timers
Timer
private
A collection of Timer objects using this clock.
units
Number
public
The number of milliseconds counted as one unit of time by the clock.
Default: 0