Fixed game startup issues and added comprehensive debug logging

- Added missing getOppositeDirection function to prevent JavaScript errors
- Enhanced initialization logging with detailed state tracking
- Added comprehensive debug logging for space button interactions
- Improved mobile control detection and setup logging
- Added state transition logging for ATTRACT → START → READY → PLAYING
- Fixed critical runtime errors preventing game from starting
This commit is contained in:
2026-02-02 18:48:04 +00:00
parent 8f0264cacb
commit 7e926c3ce2

View File

@@ -683,6 +683,17 @@
return false; // Still traveling home return false; // Still traveling home
} }
// Helper function for opposite direction
function getOppositeDirection(direction) {
switch(direction) {
case 'UP': return 'DOWN';
case 'DOWN': return 'UP';
case 'LEFT': return 'RIGHT';
case 'RIGHT': return 'LEFT';
default: return direction;
}
}
function movePacman() { function movePacman() {
if (pacman.nextDirection) { if (pacman.nextDirection) {
const nextPos = getNextPosition(pacman.x, pacman.y, pacman.nextDirection); const nextPos = getNextPosition(pacman.x, pacman.y, pacman.nextDirection);
@@ -1328,26 +1339,27 @@
} }
function handleSpaceButton() { function handleSpaceButton() {
console.log('Space button pressed, current game state:', gameState); console.log('=== SPACE BUTTON PRESSED ===');
console.log('Current game state:', gameState);
console.log('Space button element:', mobileSpaceBtnElement);
// Proper assembly-based state transitions
if (gameState === 'ATTRACT') { if (gameState === 'ATTRACT') {
// From ATTRACT mode, go to START state (show title screen) console.log('ATTRACT → START transition');
gameState = 'START'; gameState = 'START';
attractModeTimer = 0; attractModeTimer = 0;
startScreenElement.style.display = 'block'; startScreenElement.style.display = 'block';
console.log('ATTRACT → START'); console.log('ATTRACT → START completed');
soundSystem.play('ready'); soundSystem.play('ready');
} else if (gameState === 'START') { } else if (gameState === 'START') {
// From START state, go to READY state (show "READY!" for 3 seconds) console.log('START → READY transition');
gameState = 'READY'; gameState = 'READY';
readyTimer = 180; // 3 seconds at 60fps (based on assembly timing) readyTimer = 180;
startScreenElement.style.display = 'none'; startScreenElement.style.display = 'none';
resetPositions(); resetPositions();
console.log('START → READY'); console.log('START → READY completed');
soundSystem.play('ready'); soundSystem.play('ready');
} else if (gameState === 'GAME_OVER') { } else if (gameState === 'GAME_OVER') {
// From GAME_OVER, restart to START state console.log('GAME_OVER → START transition');
gameState = 'START'; gameState = 'START';
gameOverElement.style.display = 'none'; gameOverElement.style.display = 'none';
score = 0; score = 0;
@@ -1357,34 +1369,48 @@
updateLives(); updateLives();
maze = JSON.parse(JSON.stringify(mazeLayout)); maze = JSON.parse(JSON.stringify(mazeLayout));
startScreenElement.style.display = 'block'; startScreenElement.style.display = 'block';
console.log('GAME_OVER → START'); console.log('GAME_OVER → START completed');
soundSystem.play('ready'); soundSystem.play('ready');
} else if (gameState === 'PLAYING') { } else if (gameState === 'PLAYING') {
// If already playing, do nothing (or could pause)
console.log('Already in PLAYING state'); console.log('Already in PLAYING state');
} else {
console.log('Unhandled game state:', gameState);
} }
console.log('New game state:', gameState);
updateDebugInfo(); updateDebugInfo();
console.log('=== SPACE BUTTON HANDLER COMPLETE ===');
} }
// Initialize game with ATTRACT mode (assembly-based startup) // Initialize game with ATTRACT mode (assembly-based startup)
console.log('=== INITIALIZING GAME ===');
gameState = 'ATTRACT'; gameState = 'ATTRACT';
attractModeTimer = 0; attractModeTimer = 0;
console.log('Mobile detected:', isMobile);
console.log('Window width:', window.innerWidth);
// Setup mobile controls // Setup mobile controls
if (isMobile || window.innerWidth <= 768) { if (isMobile || window.innerWidth <= 768) {
console.log('Setting up mobile controls...');
setupMobileControls(); setupMobileControls();
} else {
console.log('Not setting up mobile controls - desktop mode');
} }
// Show mobile controls // Show mobile controls
mobileControlsElement.classList.add('active'); mobileControlsElement.classList.add('active');
mobileSpaceBtnElement.style.display = 'block'; mobileSpaceBtnElement.style.display = 'block';
console.log('Mobile space button element:', mobileSpaceBtnElement);
console.log('Mobile controls element:', mobileControlsElement);
// Update debug info // Update debug info
updateDebugInfo(); updateDebugInfo();
console.log('Full Pacman game initialized with ATTRACT mode'); console.log('Full Pacman game initialized with ATTRACT mode');
console.log('Game state:', gameState); console.log('Game state:', gameState);
console.log('=== GAME INITIALIZATION COMPLETE ===');
// Start game loop // Start game loop
gameLoop(); gameLoop();