Skip to content

Handling Game Events

Tymon Woźniak edited this page Apr 21, 2023 · 1 revision

How to handle events in Game

// `game` is a JSGL Game instance
// game.on(CHANNEL_NAME, FUNCTION)

CHANNEL_NAME is a string of game channel name
FUNCTION is a callback function, called at event fire.
EXAMPLE

game.on('keyUp', (event) => {
    if(event.input.isKeyUp('s')){
        game.timeScale -= 0.1;
    }
    if(event.input.isKeyUp('w')){
        game.timeScale += 0.1;
    }
    game.timeScale = JSGL.Clamp(game.timeScale, 0, 1);
});

GameEvents List

Game Channel Class Description
none GameEvent Dedicated to hadling event
start GameStartEvent Called when game starts
loadAllResources GameEvent Called when resources are fully loaded
used in gameobj. TickEvent Called every frame
draw DrawEvent Called before drawing frame
spawnedGameObject GameObjectSpawnEvent Called at game object spawn
used in gameobj. GameObjectDestroyEvent Called at game object destroy
keyDown KeyEvent Holds information for input
keyUp KeyEvent Holds information for input
mouseMove GameMouseEvent Holds information for input
mouseClick GameMouseEvent Holds information for input
mouseDown GameMouseEvent Holds information for input
mouseUp GameMouseEvent Holds information for input
mouseScroll GameMouseEvent Holds information for input

Usage Events in GameObject

GameObject uses

How to handle GameObject events

To handle GameObject events you need to override any of this function.

Function Name Params Description
Start GameObjectSpawnEvent Invoked at game object spawn.
Destroy GameObjectDestroyEvent Invoked at game object destroy.
Update TickEvent Invoked at every frame.
FixedUpdate TickEvent Invoked at last update in every frame.
Clone this wiki locally