diff --git a/CHANGELOG.md b/CHANGELOG.md index 1635a4fa..dfc384f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -287,6 +287,7 @@ obj.sprite = "bag"; - fix error where error screen was not showing when the error was thrown in a input event - fix error where fonts was cropped in the bottom +- fix an error where `stay()` object loose their input events on scene change ### v3000.1.17 diff --git a/src/game/game.ts b/src/game/game.ts index ca86f505..58a414d3 100644 --- a/src/game/game.ts +++ b/src/game/game.ts @@ -40,6 +40,7 @@ export const initGame = () => { frameEnd: []; resize: []; sceneLeave: [string]; + sceneEnter: [string]; }>(), // object events diff --git a/src/game/make.ts b/src/game/make.ts index 8400b0ed..2212b279 100644 --- a/src/game/make.ts +++ b/src/game/make.ts @@ -96,6 +96,7 @@ export function make(comps: CompList = []): GameObj { remove(obj: GameObj): void { const idx = this.children.indexOf(obj); + if (idx !== -1) { obj.parent = null; this.children.splice(idx, 1); @@ -598,8 +599,12 @@ export function make(comps: CompList = []): GameObj { const ev = app[e]?.(...args); inputEvents.push(ev); - // TODO: what if the game object is destroy and re-added obj.onDestroy(() => ev.cancel()); + obj.on("sceneEnter", () => { + ev.cancel(); + inputEvents.splice(inputEvents.indexOf(ev), 1); + app[e]?.(...args); + }); return ev; }; } diff --git a/src/game/scenes.ts b/src/game/scenes.ts index aa94696c..70768607 100644 --- a/src/game/scenes.ts +++ b/src/game/scenes.ts @@ -23,6 +23,7 @@ export function go(name: SceneName, ...args: unknown[]) { app.events.clear(); game.events.clear(); game.objEvents.clear(); + [...game.root.children].forEach((obj) => { if ( !obj.stay @@ -30,6 +31,9 @@ export function go(name: SceneName, ...args: unknown[]) { ) { game.root.remove(obj); } + else { + obj.trigger("sceneEnter", name); + } }); game.root.clearEvents();