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. It can also have its time scale freely transformed.
- 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.)
- Animations and TweenManagers can use any Clock.
Constructor
Kiwi.Time.Clock
-
manager
-
master
-
name
-
[units=1000]
Parameters:
-
manager
ClockManagerClockManager that this clock belongs to
-
master
Kiwi.Time.MasterClockMasterClock that this is getting the time in relation to
-
name
StringName of the clock
-
[units=1000]
Number optionalUnits that this clock is to operate in per second
Returns:
This Clock object
Item Index
Methods
Properties
- _currentMasterElapsed
- _elapsed
- _elapsedState
- _isPaused
- _isRunning
- _isStopped
- _lastMasterElapsed
- _maxFrameDuration
- _PAUSED static
- _RESUMED static
- _RUNNING static
- _STOPPED static
- _timeFirstStarted
- _timeLastPaused
- _timeLastStarted
- _timeLastStopped
- _timeLastUnpaused
- _totalPaused
- delta
- manager
- master
- maxFrameDuration
- name
- rate
- timers
- timeScale
- units
Methods
addTimer
-
timer
Add an existing Timer to the Clock.
Parameters:
-
timer
TimerTimer object instance to be added to this Clock
Returns:
This Clock object
checkExists
-
name
Check if the Timer already exists on this Clock.
Parameters:
-
name
StringName of the Timer
Returns:
true
if the Timer exists
createTimer
-
name
-
[delay=1]
-
[repeatCount=0]
-
[start=true]
Create a new Timer and add it to this Clock.
Parameters:
-
name
StringName of the Timer (must be unique on this Clock)
-
[delay=1]
Number optionalNumber of clock units to wait between firing events
-
[repeatCount=0]
Number optionalNumber of times to repeat the Timer (default 0)
-
[start=true]
Boolean optionalIf the timer should start
Returns:
The newly created Timer
elapsed
()
Number
public
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
Number of clock units elapsed since the clock was first started
Returns:
Number of clock units elapsed
elapsedSinceLastPaused
()
Number
public
Number of clock units elapsed since the clock was most recently paused.
Returns:
Number of clock units
elapsedSinceLastStopped
()
Number
public
Number of clock units elapsed since the clock was most recently stopped.
Returns:
Number of clock units
elapsedSinceLastUnpaused
()
Number
public
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:
"Clock"
pause
()
Kiwi.Time.Clock
public
Pause the clock. This 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 optionalTimer object you wish to remove. If you wish to delete by Timer Name set this to null.
-
[timerName='']
String optionalName of the Timer object to remove
Returns:
true
if the Timer was successfully removed
resume
()
Kiwi.Time.Clock
public
Resume the clock. This can only be resumed if it is already paused.
Returns:
This Clock object
setInterval
-
callback
-
timeout
-
[context=window]
Set a function to repeatedly execute at fixed time intervals.
Emulates window.setInterval
, except attached to a Kiwi.Time.Clock
.
This allows you to pause and manipulate time, and the timeout will
respect the clock on which it is created.
No clearInterval
is provided; you should use Kiwi.Time.Timer
functions to achieve further control.
Any parameters after context
will be passed as parameters to the
callback function. Note that you must specify context
in order for
this to work. You may specify null
, in which case it will default
to the global scope window
.
Parameters:
-
callback
FunctionFunction to execute
-
timeout
NumberMilliseconds between executions
-
[context=window]
Object optionalObject to be
this
for the callback
Returns:
Kiwi.Time.Timer object which can be used to further manipulate the timer
setTimeout
-
callback
-
timeout
-
[context]
Set a function to execute after a certain time interval.
Emulates window.setTimeout
, except attached to a Kiwi.Time.Clock
.
This allows you to pause and manipulate time, and the timeout will
respect the clock on which it is created.
No clearTimeout
is provided; you should use Kiwi.Time.Timer
functions to achieve further control.
Any parameters after context
will be passed as parameters to the
callback function. Note that you must specify context
in order for
this to work. You may specify null
, in which case it will default
to the global scope window
.
Parameters:
-
callback
FunctionFunction to execute
-
timeout
NumberMilliseconds before execution
-
[context]
Object optionalObject to be
this
for the callback
Returns:
Kiwi.Time.Timer object which can be used to further manipulate the timer
start
()
Clock
public
Start the clock. This resets the clock and starts it running.
Returns:
This Clock object
started
()
Number
public
Most recent time the clock was started relative to the master clock
Returns:
Milliseconds
stop
()
Kiwi.Time.Clock
public
Stop the clock. This 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
-
time
Convert a number to milliseconds based on clock units.
Parameters:
-
time
NumberSeconds
Returns:
Milliseconds
toString
()
String
public
Return a string representation of this object.
Returns:
String representation of the instance
update
()
public
Update all Timers linked to this Clock.
Properties
_currentMasterElapsed
Number
private
Master time on current frame
_elapsed
Number
private
Clock units elapsed since the clock was most recently started, not including paused time.
_elapsedState
Number
private
Internal reference to the state of the elapsed timer
_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
_lastMasterElapsed
Number
private
Master time on last frame
_maxFrameDuration
Number
private
Maximum frame duration. If a frame takes longer than this to render, the clock will only advance this far, in effect slowing down time. If this value is 0 or less, it will not be checked and frames can take any amount of time to render.
Default: -1
_PAUSED
Number
private
static
Constant indicating that the Clock is paused
Default: 1
_RESUMED
Number
private
static
Constant indicating that the Clock is running (and has been paused then resumed)
Default: 2
_RUNNING
Number
private
static
Constant indicating that the Clock is running (and has not yet been paused and resumed)
Default: 0
_STOPPED
Number
private
static
Constant indicating that the Clock is stopped
Default: 3
_timeFirstStarted
Number
private
Time the clock was first started relative to the master clock
Default: null
_timeLastPaused
Number
private
Time the clock was most receently paused relative to the master clock.
Default: null
_timeLastStarted
Number
private
Most recent time the clock was started relative to the master clock
Default: null
_timeLastStopped
Number
private
Time the clock was most recently stopped relative to the master clock.
Default: null
_timeLastUnpaused
Number
private
Time the clock was most recently unpaused relative to the master clock.
Default: null
_totalPaused
Number
private
Total number of milliseconds the clock has been paused since it was last started
Default: 0
delta
Number
public
The time it takes for the time to update. Using this you can calculate the fps.
manager
ClockManager
public
Time manager that this clock belongs to
maxFrameDuration
Number
public
Maximum frame duration. If a frame takes longer than this to render, the clock will only advance this far, in effect slowing down time. If this value is 0 or less, it will not be checked and frames can take any amount of time to render.
Default: -1
name
String
public
Name of the clock
rate
Number
public
Rate of time passage, as modified by time scale and frame rate. Under ideal conditions this should be 1. If the frame rate drops, this will rise. Multiply transformations by rate to get smooth change over time.
timers
Timer
private
Collection of Timer objects using this clock
timeScale
Number
public
Rate at which time passes on this clock. 1 is normal speed. 1.5 is faster. 0 is no speed. -1 is backwards. This mostly affects timers, animations and tweens.
Default: 1.0
units
Number
public
Number of milliseconds counted as one unit of time by the clock
Default: 0