Small helpers for timing, and the game's version number. They can be used anywhere in a script, without a manager's name in front.
| Member | Description |
|---|---|
delay() |
Waits for some milliseconds |
invoke() |
Runs a function after some milliseconds |
nextFrame() |
Runs a function on the next frame |
appVersion |
The game's version number |
delay
delay(ms: number): Promise<void>
Returns a promise that resolves after ms milliseconds. await it inside an async method to pause that method, while the rest of the game keeps running.
How it works
It keeps counting after the scene has gone. Check the scene is still showing before changing its elements after a long wait.
Example
async onCorrectAnswer() {
this.dom.tick.show();
await delay(1500); // a second and a half
this.dom.tick.hide();
this.nextQuestion();
},
invoke
invoke(fn: () => void, ms = 0): number
Runs fn after ms milliseconds, without waiting for it. With no time, it runs once the current code has finished.
Returns an id that clearTimeout takes, to cancel it before it runs.
Example
hintTimer: 0,
onStart() {
this.hintTimer = invoke(() => this.dom.hint.show(), 10000); // show a hint after ten seconds
},
onExit() {
clearTimeout(this.hintTimer); // don't show it if the player has already left
},
nextFrame
nextFrame(fn: (time: number) => void): number
Runs fn just before the next frame is drawn, usually 60 times a second. fn receives the time in milliseconds. Call nextFrame again inside fn to run every frame.
Returns an id that cancelAnimationFrame takes, to cancel it.
Example
frameId: 0,
angle: 0,
onStart() {
const spin = () => {
this.angle += 2;
this.dom.wheel.style.rotate = `${this.angle}deg`;
this.frameId = nextFrame(spin); // again on the next frame
};
spin();
},
onExit() {
cancelAnimationFrame(this.frameId);
},
appVersion
appVersion: string
The game's version number, from Version in Project Settings, such as "1.0.2".
Example
onEnter() {
this.dom.version_label.textContent = `v${appVersion}`;
},