Crafty.timer

Handles game ticks

Methods

Back to top

Crafty.timer.FPS()

public void Crafty.timer.FPS()

Returns the target frames per second. This is not an actual frame rate.

public void Crafty.timer.FPS(Number value)
value

the target rate

Events

FPSChange [new target FPS = {Number}]
Triggered when the target FPS is changed by user

Sets the target frames per second. This is not an actual frame rate. The default rate is 50.

See Also

Back to top

Crafty.timer.simulateFrames()

public this Crafty.timer.simulateFrames(Number frames[, Number timestep])

Advances the game state by a number of frames and draws the resulting stage at the end. Useful for tests and debugging.

frames

number of frames to simulate

timestep

the duration to pass each frame. Defaults to milliSecPerFrame (20 ms) if not specified.

Back to top

Crafty.timer.step()

public void Crafty.timer.step()

Events

EnterFrame [Data = { frame: Number, dt:Number }]
Triggered before each frame. Passes the frame number, and the amount of time since the last frame. If the time is greater than maxTimestep, that will be used instead. (The default value of maxTimestep is 50 ms.)
UpdateFrame [Data = { frame: Number, dt:Number }]
Triggered on each frame. Passes the frame number, and the amount of time since the last frame. If the time is greater than maxTimestep, that will be used instead. (The default value of maxTimestep is 50 ms.)
ExitFrame [Data = { frame: Number, dt:Number }]
Triggered after each frame. Passes the frame number, and the amount of time since the last frame. If the time is greater than maxTimestep, that will be used instead. (The default value of maxTimestep is 50 ms.)
PreRender
Triggered every time immediately before a scene should be rendered
RenderScene
Triggered every time a scene should be rendered
PostRender
Triggered every time immediately after a scene should be rendered
MeasureWaitTime [Data = {Number}]
Triggered at the beginning of each step after the first. Passes the time the game loop waited between steps.
MeasureFrameTime [Data = {Number}]
Triggered after each frame. Passes the time it took to advance one frame.
MeasureRenderTime [Data = {Number}]
Triggered after each render. Passes the time it took to render the scene

Advances the game by performing a step. A step consists of one/multiple frames followed by a render. The amount of frames depends on the timer's steptype. Specifically it triggers EnterFrame, UpdateFrame & ExitFrame events for each frame and PreRender, RenderScene & PostRender events for each render.

Back to top

Crafty.timer.steptype()

Events

NewSteptype [New steptype = { mode, maxTimeStep }]
when the current steptype changes

Can be called to set the type of timestep the game loop uses.

public void Crafty.timer.steptype(mode [, maxTimeStep])
mode

the type of time loop. Allowed values are "fixed", "semifixed", and "variable". Crafty defaults to "fixed".

maxTimeStep

For "fixed", sets the max number of frames per step. For "variable" and "semifixed", sets the maximum time step allowed.

Can be called to get the type of timestep the game loop uses.

public Object Crafty.timer.steptype(void)
[Returns]

Object containing the current timestep's properties { mode, maxTimeStep }

  • In "fixed" mode, each frame is sent the same value of dt, and to achieve the target game speed, mulitiple frame events are triggered before each render.
  • In "variable" mode, there is only one frame triggered per render. This recieves a value of dt equal to the actual elapsed time since the last frame.
  • In "semifixed" mode, multiple frames per render are processed, and the total time since the last frame is divided evenly between them.

See Also