Skip to main content

GameLoop

The GameLoop class is a singleton that handles the game loop. It manages update and render subscribers, as well as calculating the delta time and frame rate.

info

Prefer to get the unique instance of this service from the ServiceContainer like:

const gameLoop = ServiceContainer.gameLoop;

Getters

instance

  • Type: GameLoop

Returns the singleton instance of the GameLoop class.

FPS

  • Type: number

Returns the current frames per second (FPS) as a number.


Methods

setFreezeLimit

prototype
setFreezeLimit(limit: number): GameLoop

Sets the freeze limit in seconds.

Parameters

  • limit
    • Type: number
    • The freeze limit in seconds

Return

GameLoop The instance of the GameLoop class.

Example

ServiceContainer.GameLoop.setFreezeLimit(0.1);

subscribeToUpdate

prototype
subscribeToUpdate(subscriber: UpdateFunction): GameLoop

Subscribes a function to the update loop.

Parameters

  • subscriber
    • Type: UpdateFunction
    • The function to subscribe

Return

GameLoop The instance of the GameLoop class.

Example

ServiceContainer.GameLoop.subscribeToUpdate((deltaTime) => {
console.log(`deltaTime: ${deltaTime}`);
});

unsubscribeFromUpdate

prototype
unsubscribeFromUpdate(subscriber: UpdateFunction): GameLoop

Unsubscribes a function from the update loop.

Parameters

  • subscriber
    • Type: UpdateFunction
    • The function to unsubscribe

Return

GameLoop The instance of the GameLoop class.

Example

const updateFunction = (deltaTime) => {
console.log(`deltaTime: ${deltaTime}`);
};
const gameLoop = ServiceContainer.GameLoop;
gameLoop.subscribeToUpdate(updateFunction);
//...
gameLoop.unsubscribeFromUpdate(updateFunction);

subscribeToPreRender

prototype
subscribeToPreRender(subscriber: DrawFunction): GameLoop

Subscribes a function to the pre-render loop.

Parameters

  • subscriber
    • Type: DrawFunction
    • The function to subscribe

Return

GameLoop The instance of the GameLoop class.

Example

ServiceContainer.GameLoop.subscribeToPreRender((ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
});

unsubscribeFromPreRender

prototype
unsubscribeFromPreRender(subscriber: DrawFunction): GameLoop

Unsubscribes a function to the pre-render loop.

Parameters

  • subscriber
    • Type: DrawFunction
    • The function to unsubscribe

Return

GameLoop The instance of the GameLoop class.

Example

const preRenderFunction = (ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
};
const gameLoop = ServiceContainer.GameLoop;
gameLoop.subscribeToPreRender(preRenderFunction);
//...
gameLoop.unsubscribeFromPreRender(preRenderFunction);

subscribeToPostRender

prototype
subscribeToPostRender(subscriber: DrawFunction): GameLoop

Subscribes a function to the post-render loop.

Parameters

  • subscriber
    • Type: DrawFunction
    • The function to subscribe

Return

GameLoop The instance of the GameLoop class.

Example

ServiceContainer.GameLoop.subscribeToPostRender((ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
});

unsubscribeFromPostRender

prototype
unsubscribeFromPostRender(subscriber: DrawFunction): GameLoop

Unsubscribes a function from the post-render loop.

Parameters

  • subscriber
    • Type: DrawFunction
    • The function to unsubscribe

Return

GameLoop The instance of the GameLoop class.

Example

const postRenderFunction = (ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
};
const gameLoop = ServiceContainer.GameLoop;
gameLoop.subscribeToPostRender(postRenderFunction);
//...
gameLoop.unsubscribeFromPostRender(postRenderFunction);