Browse Source

Fixed game tick interval continuing after game exit

Arnaud Vergnet 3 years 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
 
109
 
110
     onClock(callback: Function) {
110
     onClock(callback: Function) {
111
         this.#gameTime++;
111
         this.#gameTime++;
112
-        console.log(this.#gameTime);
113
         callback(this.#gameTime);
112
         callback(this.#gameTime);
114
     }
113
     }
115
 
114
 
206
         }
205
         }
207
     }
206
     }
208
 
207
 
209
-    endGame(isRestart: boolean) {
208
+    stopGame() {
210
         this.#gameRunning = false;
209
         this.#gameRunning = false;
211
         this.#gamePaused = false;
210
         this.#gamePaused = false;
212
         clearInterval(this.#gameTickInterval);
211
         clearInterval(this.#gameTickInterval);
213
         clearInterval(this.#gameTimeInterval);
212
         clearInterval(this.#gameTimeInterval);
213
+    }
214
+
215
+    endGame(isRestart: boolean) {
216
+        this.stopGame();
214
         this.endCallback(this.#gameTime, this.#scoreManager.getScore(), isRestart);
217
         this.endCallback(this.#gameTime, this.#scoreManager.getScore(), isRestart);
215
     }
218
     }
216
 
219
 

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

67
         this.startGame();
67
         this.startGame();
68
     }
68
     }
69
 
69
 
70
+    componentWillUnmount() {
71
+        this.logic.stopGame();
72
+    }
73
+
70
     getRightButton = () => {
74
     getRightButton = () => {
71
         return <MaterialHeaderButtons>
75
         return <MaterialHeaderButtons>
72
             <Item title="pause" iconName="pause" onPress={this.togglePause}/>
76
             <Item title="pause" iconName="pause" onPress={this.togglePause}/>
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
     startGame = () => {
175
     startGame = () => {
198
         this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
176
         this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
199
         this.setState({
177
         this.setState({

Loading…
Cancel
Save