mirror of
https://github.com/DeNNiiInc/Connect-5.git
synced 2026-04-17 20:36:00 +00:00
Fix variable scope: Ensure global access to game and multiplayer client
This commit is contained in:
5
game.js
5
game.js
@@ -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();
|
||||
});
|
||||
|
||||
23
index.html
23
index.html
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user