API Docs for: 1.2.0
Show:

Kiwi.Time.Clock Class

Defined in: src/time/Clock.ts:10
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

Parameters:

  • manager ClockManager

    The ClockManager that this clock belongs to.

  • master Kiwi.Time.MasterClock

    The MasterClock that it is getting the time in relation to.

  • name String

    The name of the clock.

  • [units=1000] Number optional

    The 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

    The Timer object instance to be added to ths Clock.

Returns:

Kiwi.Time.Clock:

This Clock object.

checkExists

(
  • name
)
Boolean public

Check if the Timer already exists on this Clock

Parameters:

  • name String

    The name of the Timer.

Returns:

Boolean:

true if the Timer exists, false if not.

createTimer

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

Creates a new Timer and adds it to this Clock.

Parameters:

  • name String

    The name of the Timer (must be unique on this Clock).

  • [delay=1] Number optional

    The number of clock units to wait between firing events (default 1)

  • [repeatCount=0] Number optional

    The number of times to repeat this Timer (default 0)

  • [start=true] Boolean optional

    If the timer should start.

Returns:

Kiwi.Time.Timer:

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:

number of clock units.

elapsedSinceFirstStarted.

() Number public

The number of clock units elapsed since the clock was first started.

Returns:

Number:

number of clock units.

elapsedSinceLastPaused.

() Number public

The number of clock units elapsed since the clock was most recently paused.

Returns:

Number:

number of clock units.

elapsedSinceLastStopped.

() Number public

The number of clock units elapsed since the clock was most recently stopped.

Returns:

Number:

number of clock units.

elapsedSinceLastUnpaused.

() Number public

The 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. The clock 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

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

  • [timerName=''] String optional

    The name of the Timer object to remove.

Returns:

Boolean:

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:

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

Get the most recent time the clock was started relative to the master clock.

Returns:

Number:

milliseconds.

stop

() Kiwi.Time.Clock public

Stop the clock. Clock 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

Returns a string representation of this object.

Returns:

String:

a string representation of the instance.

update

() public

Updates all Timers linked to this Clock.

Properties

_currentMasterElapsed

Number private

Defined in src/time/Clock.ts:138

Available since 1.2.0

Master time on current frame

_elapsed

Number private

Defined in src/time/Clock.ts:119

Available since 1.2.0

Clock units elapsed since the clock was most recently started, not including paused time.

_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

_lastMasterElapsed

Number private

Defined in src/time/Clock.ts:129

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

_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.

master

Kiwi.Time.MasterClock public

The master clock.

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:148

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

A collection of Timer objects using this clock.

timeScale

Number public

Defined in src/time/Clock.ts:107

Available since 1.2.0

Rate at which time passes on this clock. 1 is normal speed. 0 is no speed. -1 is backwards. This mostly affects timers, animations and tweens.

Default: 1.0

units

Number public

The number of milliseconds counted as one unit of time by the clock.

Default: 0