From bc76e7aab1bbab72acfa3558f64fbfc041117f9d Mon Sep 17 00:00:00 2001 From: DeNNiiInc Date: Sun, 14 Dec 2025 12:07:13 +1100 Subject: [PATCH] FIX: Prevent multiplayer initialization before game is ready - Add polling mechanism to wait for window.game initialization - Disable multiplayer button until game is fully initialized - Enable button in DOMContentLoaded after game creation - Prevents 'Game instance not found' error on production This fixes the critical issue where clicking Multiplayer too quickly would fail because window.game wasn't ready yet. --- game.js | 8 +++++++- index.html | 13 +++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/game.js b/game.js index 421a6a6..35c538a 100644 --- a/game.js +++ b/game.js @@ -247,9 +247,15 @@ class Connect5Game { } } -// Initialize game when DOM is loaded // Initialize game when DOM is loaded window.game = null; document.addEventListener("DOMContentLoaded", () => { window.game = new Connect5Game(); + + // Enable multiplayer button now that game is ready + const multiplayerBtn = document.getElementById('multiplayerModeBtn'); + if (multiplayerBtn) { + multiplayerBtn.disabled = false; + console.log('✅ Game initialized, multiplayer button enabled'); + } }); diff --git a/index.html b/index.html index 9fb7b7a..b0b01fa 100644 --- a/index.html +++ b/index.html @@ -48,7 +48,7 @@ - @@ -217,7 +217,16 @@ // Initialize multiplayer if not already if (!window.multiplayerClient) { if (!window.game) { - console.error("Game instance not found!"); + console.log('⏳ Waiting for game to initialize...'); + // Poll for game to be ready + const checkGame = setInterval(() => { + if (window.game) { + clearInterval(checkGame); + console.log('✅ Game ready, initializing multiplayer...'); + window.multiplayerClient = new MultiplayerClient(window.game); + window.multiplayerClient.connect(); + } + }, 100); // Check every 100ms return; } window.multiplayerClient = new MultiplayerClient(window.game);