@@ -114,7 +114,7 @@ class Game {
114114 }
115115
116116 gameOver ( ) {
117- // Switch to intro screen with game over flag
117+ // Create new intro screen with game over state
118118 this . screens . intro = new IntroScreen ( this . ctx , {
119119 virtualWidth : this . virtualWidth ,
120120 virtualHeight : this . virtualHeight ,
@@ -123,7 +123,7 @@ class Game {
123123 finalScore : this . gameState . score ,
124124 highScore : this . gameState . highScore
125125 } ) ;
126- this . currentScreen = 'intro' ;
126+ this . switchScreen ( 'intro' ) ;
127127 }
128128
129129 reset ( ) {
@@ -133,26 +133,29 @@ class Game {
133133 }
134134
135135 switchScreen ( screenName ) {
136- // Start music when transitioning from startup to intro
137- if ( this . currentScreen === 'startup' && screenName === 'intro' ) {
138- this . musicPlayer . start ( ) . then ( ( ) => {
139- this . musicPlayer . fadeIn ( 3 ) ;
140- } ) ;
136+ // Cleanup current screen
137+ if ( this . screens [ this . currentScreen ] ?. cleanup ) {
138+ this . screens [ this . currentScreen ] . cleanup ( ) ;
141139 }
142140
143- // Fade out music when going to game over
144- if ( screenName === 'intro' && this . screens [ this . currentScreen ] ?. isGameOver ) {
145- this . musicPlayer . fadeOut ( 2 ) ;
141+ // Special handling for game screen
142+ if ( screenName === 'game' ) {
143+ // Always create a new game screen instance
144+ this . screens . game = new GameScreen ( this . ctx , {
145+ virtualWidth : this . virtualWidth ,
146+ virtualHeight : this . virtualHeight ,
147+ bgScroller : this . bgScroller ,
148+ gameState : this . gameState
149+ } ) ;
146150 }
147151
148- // Cleanup current screen if it exists
149- if ( this . screens [ this . currentScreen ] ?. cleanup ) {
150- this . screens [ this . currentScreen ] . cleanup ( ) ;
152+ // Handle music transitions
153+ if ( this . currentScreen === 'startup' && screenName === 'intro' ) {
154+ this . musicPlayer . start ( ) . then ( ( ) => {
155+ this . musicPlayer . fadeIn ( 3 ) ;
156+ } ) ;
151157 }
152158
153- if ( screenName === 'game' && ! this . screens . game ) {
154- this . initGameScreen ( ) ;
155- }
156159 this . currentScreen = screenName ;
157160 this . inputManager . setCurrentScreen ( screenName ) ;
158161 }
0 commit comments