Fix variable scope: Ensure global access to game and multiplayer client

This commit is contained in:
2025-12-13 20:34:07 +11:00
parent dbc3f40019
commit b57611f0c4
3 changed files with 22 additions and 9 deletions

View File

@@ -248,7 +248,8 @@ class Connect5Game {
}
// Initialize game when DOM is loaded
let game;
// Initialize game when DOM is loaded
window.game = null;
document.addEventListener("DOMContentLoaded", () => {
game = new Connect5Game();
window.game = new Connect5Game();
});

View File

@@ -203,8 +203,8 @@
statusMessage.style.display = 'block';
// Reset to local mode
if (multiplayerClient) {
multiplayerClient.isMultiplayer = false;
if (window.multiplayerClient) {
window.multiplayerClient.isMultiplayer = false;
}
} else {
localBtn.classList.remove('active');
@@ -215,9 +215,13 @@
statusMessage.style.display = 'none';
// Initialize multiplayer if not already
if (!multiplayerClient) {
multiplayerClient = new MultiplayerClient(game);
multiplayerClient.connect();
if (!window.multiplayerClient) {
if (!window.game) {
console.error("Game instance not found!");
return;
}
window.multiplayerClient = new MultiplayerClient(window.game);
window.multiplayerClient.connect();
}
}
}
@@ -247,7 +251,14 @@
}
error.style.display = 'none';
multiplayerClient.registerPlayer(username);
if (window.multiplayerClient) {
window.multiplayerClient.registerPlayer(username);
} else {
console.error("Multiplayer client not initialized");
error.textContent = "Error: Multiplayer not initialized. Refresh page.";
error.style.display = 'block';
}
}
// Change username

View File

@@ -507,4 +507,5 @@ class MultiplayerClient {
}
// Initialize multiplayer client (will be used by game.js)
let multiplayerClient = null;
// Initialize multiplayer client (will be used by game.js)
window.multiplayerClient = null;