@@ -151,7 +151,7 @@ class Game {
151151 }
152152
153153 gameOver ( ) {
154- // Switch to intro screen with game over flag
154+ // Create new intro screen with game over state
155155 this . screens . intro = new IntroScreen ( this . ctx , {
156156 virtualWidth : this . virtualWidth ,
157157 virtualHeight : this . virtualHeight ,
@@ -160,7 +160,7 @@ class Game {
160160 finalScore : this . gameState . score ,
161161 highScore : this . gameState . highScore
162162 } ) ;
163- this . currentScreen = 'intro' ;
163+ this . switchScreen ( 'intro' ) ;
164164 }
165165
166166 reset ( ) {
@@ -170,26 +170,29 @@ class Game {
170170 }
171171
172172 switchScreen ( screenName ) {
173- // Start music when transitioning from startup to intro
174- if ( this . currentScreen === 'startup' && screenName === 'intro' ) {
175- this . musicPlayer . start ( ) . then ( ( ) => {
176- this . musicPlayer . fadeIn ( 3 ) ;
177- } ) ;
173+ // Cleanup current screen
174+ if ( this . screens [ this . currentScreen ] ?. cleanup ) {
175+ this . screens [ this . currentScreen ] . cleanup ( ) ;
178176 }
179177
180- // Fade out music when going to game over
181- if ( screenName === 'intro' && this . screens [ this . currentScreen ] ?. isGameOver ) {
182- this . musicPlayer . fadeOut ( 2 ) ;
178+ // Special handling for game screen
179+ if ( screenName === 'game' ) {
180+ // Always create a new game screen instance
181+ this . screens . game = new GameScreen ( this . ctx , {
182+ virtualWidth : this . virtualWidth ,
183+ virtualHeight : this . virtualHeight ,
184+ bgScroller : this . bgScroller ,
185+ gameState : this . gameState
186+ } ) ;
183187 }
184188
185- // Cleanup current screen if it exists
186- if ( this . screens [ this . currentScreen ] ?. cleanup ) {
187- this . screens [ this . currentScreen ] . cleanup ( ) ;
189+ // Handle music transitions
190+ if ( this . currentScreen === 'startup' && screenName === 'intro' ) {
191+ this . musicPlayer . start ( ) . then ( ( ) => {
192+ this . musicPlayer . fadeIn ( 3 ) ;
193+ } ) ;
188194 }
189195
190- if ( screenName === 'game' && ! this . screens . game ) {
191- this . initGameScreen ( ) ;
192- }
193196 this . currentScreen = screenName ;
194197 this . inputManager . setCurrentScreen ( screenName ) ;
195198 }
0 commit comments