API Docs for: 1.4.0
Show:

Kiwi.Time.Clock Class

Defined in: src\time\Clock.ts:8
Module: Time
Parent Module: Kiwi

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]
)
Kiwi.Time.Clock

Defined in src\time\Clock.ts:8

Parameters:

  • manager ClockManager

    ClockManager that this clock belongs to

  • master Kiwi.Time.MasterClock

    MasterClock that this is getting the time in relation to

  • name String

    Name of the clock

  • [units=1000] Number optional

    Units that this clock is to operate in per second

Returns:

Kiwi.Time.Clock:

This Clock object

Methods

addTimer

(
  • timer
)
Kiwi.Time.Clock public

Add an existing Timer to the Clock.

Parameters:

  • timer Timer

    Timer object instance to be added to this Clock

Returns:

Kiwi.Time.Clock:

This Clock object

checkExists

(
  • name
)
Boolean public

Check if the Timer already exists on this Clock.

Parameters:

  • name String

    Name of the Timer

Returns:

Boolean:

true if the Timer exists

createTimer

(
  • name
  • [delay=1]
  • [repeatCount=0]
  • [start=true]
)
Kiwi.Time.Timer public

Create a new Timer and add it to this Clock.

Parameters:

  • name String

    Name of the Timer (must be unique on this Clock)

  • [delay=1] Number optional

    Number of clock units to wait between firing events

  • [repeatCount=0] Number optional

    Number of times to repeat the Timer (default 0)

  • [start=true] Boolean optional

    If the timer should start

Returns:

Kiwi.Time.Timer:

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:

Number of clock units

elapsedSinceFirstStarted

() Number public

Number of clock units elapsed since the clock was first started

Returns:

Number:

Number of clock units elapsed

elapsedSinceLastPaused

() Number public

Number of clock units elapsed since the clock was most recently paused.

Returns:

Number:

Number of clock units

elapsedSinceLastStopped

() Number public

Number of clock units elapsed since the clock was most recently stopped.

Returns:

Number:

Number of clock units

elapsedSinceLastUnpaused

() Number public

Number of clock units elapsed since the clock was most recently unpaused.

Returns:

Number:

Number of clock units

isPaused

() Boolean public

Check if the clock is in the paused state

Returns:

Boolean:

true if paused

isRunning

() Boolean public

Check if the clock is currently running

Returns:

Boolean:

true if running

isStopped

() Boolean public

Check if the clock is in the stopped state

Returns:

Boolean:

true if stopped

objType

() String public

The type of object that this is

Returns:

String:

"Clock"

pause

() Kiwi.Time.Clock public

Pause the clock. This can only be paused if it is already running.

Returns:

Kiwi.Time.Clock:

This Clock object

removeTimer

(
  • [timer=null]
  • [timerName='']
)
Boolean public

Remove a Timer from this Clock based on either the Timer object or its name.

Parameters:

  • [timer=null] Timer optional

    Timer object you wish to remove. If you wish to delete by Timer Name set this to null.

  • [timerName=''] String optional

    Name of the Timer object to remove

Returns:

Boolean:

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:

Kiwi.Time.Clock:

This Clock object

setInterval

(
  • callback
  • timeout
  • [context=window]
)
Kiwi.Time.Timer public

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 Function

    Function to execute

  • timeout Number

    Milliseconds between executions

  • [context=window] Object optional

    Object to be this for the callback

Returns:

Kiwi.Time.Timer:

Kiwi.Time.Timer object which can be used to further manipulate the timer

setTimeout

(
  • callback
  • timeout
  • [context]
)
Kiwi.Time.Timer public

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 Function

    Function to execute

  • timeout Number

    Milliseconds before execution

  • [context] Object optional

    Object to be this for the callback

Returns:

Kiwi.Time.Timer:

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:

Clock:

This Clock object

started

() Number public

Most recent time the clock was started relative to the master clock

Returns:

Number:

Milliseconds

stop

() Kiwi.Time.Clock public

Stop the clock. This can only be stopped if it is already running or is paused.

Returns:

Kiwi.Time.Clock:

This Clock object

stopAllTimers

() Clock public

Stop all timers attached to the clock.

Returns:

Clock:

This Clock object

toMilliseconds

(
  • time
)
Number public

Convert a number to milliseconds based on clock units.

Parameters:

  • time Number

    Seconds

Returns:

Number:

Milliseconds

toString

() String public

Return a string representation of this object.

Returns:

String:

String representation of the instance

update

() public

Update all Timers linked to this Clock.

Properties

_currentMasterElapsed

Number private

Defined in src\time\Clock.ts:150

Available since 1.2.0

Master time on current frame

_elapsed

Number private

Defined in src\time\Clock.ts:131

Available since 1.2.0

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

Defined in src\time\Clock.ts:141

Available since 1.2.0

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

Defined in src\time\Clock.ts:382

Available since 1.3.0

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

master

Kiwi.Time.MasterClock public

Master clock from which time is derived

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

Defined in src\time\Clock.ts:160

Available since 1.2.0

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

Defined in src\time\Clock.ts:119

Available since 1.2.0

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