Skip to content

Commit

Permalink
fix: stay loose events (#434) (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel authored Oct 6, 2024
1 parent 9033fc7 commit 8e9bee1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const initGame = () => {
frameEnd: [];
resize: [];
sceneLeave: [string];
sceneEnter: [string];
}>(),

// object events
Expand Down
7 changes: 6 additions & 1 deletion src/game/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function make<T>(comps: CompList<T> = []): GameObj<T> {

remove(obj: GameObj): void {
const idx = this.children.indexOf(obj);

if (idx !== -1) {
obj.parent = null;
this.children.splice(idx, 1);
Expand Down Expand Up @@ -598,8 +599,12 @@ export function make<T>(comps: CompList<T> = []): GameObj<T> {
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;
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/game/scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ export function go(name: SceneName, ...args: unknown[]) {
app.events.clear();
game.events.clear();
game.objEvents.clear();

[...game.root.children].forEach((obj) => {
if (
!obj.stay
|| (obj.scenesToStay && !obj.scenesToStay.includes(name))
) {
game.root.remove(obj);
}
else {
obj.trigger("sceneEnter", name);
}
});

game.root.clearEvents();
Expand Down

0 comments on commit 8e9bee1

Please sign in to comment.