mirror of
https://github.com/Xevion/Pac-Man.git
synced 2026-01-31 08:25:06 -06:00
fix(web): prevent stopGame from being called on non-game page transitions
Only stop the game when navigating away from the game page (/) to avoid WASM runtime errors when navigating between /leaderboard and /download. Add error handling to gracefully handle crashes during page transitions.
This commit is contained in:
@@ -23,7 +23,11 @@ function restartGame() {
|
||||
const win = getPacmanWindow();
|
||||
const module = win.Module;
|
||||
|
||||
if (module?._restart_game) {
|
||||
if (!module?._restart_game) {
|
||||
console.warn("Game restart function not available (WASM may not be initialized)");
|
||||
return;
|
||||
}
|
||||
|
||||
const canvas = document.getElementById("canvas") as HTMLCanvasElement | null;
|
||||
if (!canvas) {
|
||||
console.error("Canvas element not found during game restart");
|
||||
@@ -41,5 +45,4 @@ function restartGame() {
|
||||
} catch (error) {
|
||||
console.error("Failed to restart game:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,11 +10,18 @@ export const onPageTransitionStart: OnPageTransitionStartAsync = async (pageCont
|
||||
setPendingNavigation(pageContext.urlPathname);
|
||||
document.querySelector("body")?.classList.add("page-is-transitioning");
|
||||
|
||||
// Stop the game loop when navigating away from the game page
|
||||
// Only stop the game when navigating AWAY FROM the game page
|
||||
// Don't stop when navigating between other pages (e.g., /leaderboard <-> /download)
|
||||
if (window.location.pathname === "/") {
|
||||
const win = getPacmanWindow();
|
||||
if (win.Module?._stop_game) {
|
||||
try {
|
||||
console.log("Stopping game loop for page transition");
|
||||
win.Module._stop_game();
|
||||
} catch (error) {
|
||||
console.warn("Failed to stop game (game may have already crashed):", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for fade-out animation to complete before page content changes
|
||||
|
||||
Reference in New Issue
Block a user