useSceneUnmountfunctionhooknacatamalon
useSceneUnmount(cleanup: () => void) => voidRuns your code once, the moment the scene disappears: when it is stopped, when it changes to another scene, or when the whole game is destroyed. Pausing is not leaving, so a paused scene does not run it.
You need it for what the scene started outside the engine, because that keeps running on
its own after the scene is gone: a setInterval, a keydown listener on the window, a request
you want to abort. Everything the engine gave you (its sprites, its useUpdate, its loaded
images) goes away with the scene and needs no cleaning.
Register as many as you like; if one of them throws, it is reported and the rest still run.
Parámetros
cleanup() => voidWhat to undo. It receives nothing and is called exactly once.
Ejemplo
export const Level: TSceneFn = () => {
const onKey = (event: KeyboardEvent) => console.log(event.key);
window.addEventListener('keydown', onKey);
// Without this, the listener stays alive after the level ends and fires in the next scene.
useSceneUnmount(() => window.removeEventListener('keydown', onKey));
return createScene();
};