API Docs for: 1.0.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.
  • 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]
)
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.

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:

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.

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.

() Number public

Convert a number to milliseconds based on clock units.

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

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

master

Kiwi.Time.MasterClock public

The master clock.

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