Browse Source

Fixed game tick interval continuing after game exit

Arnaud Vergnet 1 year ago
parent
commit
9f4a8c837d
2 changed files with 9 additions and 28 deletions
  1. 5
    2
      src/screens/Game/logic/GameLogic.js
  2. 4
    26
      src/screens/Game/screens/GameMainScreen.js

+ 5
- 2
src/screens/Game/logic/GameLogic.js View File

@@ -109,7 +109,6 @@ export default class GameLogic {
109 109
 
110 110
     onClock(callback: Function) {
111 111
         this.#gameTime++;
112
-        console.log(this.#gameTime);
113 112
         callback(this.#gameTime);
114 113
     }
115 114
 
@@ -206,11 +205,15 @@ export default class GameLogic {
206 205
         }
207 206
     }
208 207
 
209
-    endGame(isRestart: boolean) {
208
+    stopGame() {
210 209
         this.#gameRunning = false;
211 210
         this.#gamePaused = false;
212 211
         clearInterval(this.#gameTickInterval);
213 212
         clearInterval(this.#gameTimeInterval);
213
+    }
214
+
215
+    endGame(isRestart: boolean) {
216
+        this.stopGame();
214 217
         this.endCallback(this.#gameTime, this.#scoreManager.getScore(), isRestart);
215 218
     }
216 219
 

+ 4
- 26
src/screens/Game/screens/GameMainScreen.js View File

@@ -67,6 +67,10 @@ class GameMainScreen extends React.Component<Props, State> {
67 67
         this.startGame();
68 68
     }
69 69
 
70
+    componentWillUnmount() {
71
+        this.logic.stopGame();
72
+    }
73
+
70 74
     getRightButton = () => {
71 75
         return <MaterialHeaderButtons>
72 76
             <Item title="pause" iconName="pause" onPress={this.togglePause}/>
@@ -168,32 +172,6 @@ class GameMainScreen extends React.Component<Props, State> {
168 172
         });
169 173
     }
170 174
 
171
-    showGameOverConfirm() {
172
-        let message = i18n.t("screens.game.gameOver.score") + this.state.gameScore + '\n';
173
-        message += i18n.t("screens.game.gameOver.level") + this.state.gameLevel + '\n';
174
-        message += i18n.t("screens.game.gameOver.time") + this.getFormattedTime(this.state.gameTime) + '\n';
175
-        const onDismiss = () => {
176
-            this.onDialogDismiss();
177
-            this.startGame();
178
-        };
179
-        this.setState({
180
-            dialogVisible: true,
181
-            dialogTitle: i18n.t("screens.game.gameOver.text"),
182
-            dialogMessage: message,
183
-            dialogButtons: [
184
-                {
185
-                    title: i18n.t("screens.game.gameOver.exit"),
186
-                    onPress: () => this.props.navigation.goBack()
187
-                },
188
-                {
189
-                    title: i18n.t("screens.game.resume"),
190
-                    onPress: onDismiss
191
-                }
192
-            ],
193
-            onDialogDismiss: onDismiss,
194
-        });
195
-    }
196
-
197 175
     startGame = () => {
198 176
         this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
199 177
         this.setState({

Loading…
Cancel
Save