From 9f4a8c837d395625d0ef34a81520406cc189c450 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Tue, 21 Jul 2020 22:45:45 +0200 Subject: [PATCH] Fixed game tick interval continuing after game exit --- src/screens/Game/logic/GameLogic.js | 7 +++-- src/screens/Game/screens/GameMainScreen.js | 30 +++------------------- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/screens/Game/logic/GameLogic.js b/src/screens/Game/logic/GameLogic.js index d562e6f..8c2292d 100644 --- a/src/screens/Game/logic/GameLogic.js +++ b/src/screens/Game/logic/GameLogic.js @@ -109,7 +109,6 @@ export default class GameLogic { onClock(callback: Function) { this.#gameTime++; - console.log(this.#gameTime); callback(this.#gameTime); } @@ -206,11 +205,15 @@ export default class GameLogic { } } - endGame(isRestart: boolean) { + stopGame() { this.#gameRunning = false; this.#gamePaused = false; clearInterval(this.#gameTickInterval); clearInterval(this.#gameTimeInterval); + } + + endGame(isRestart: boolean) { + this.stopGame(); this.endCallback(this.#gameTime, this.#scoreManager.getScore(), isRestart); } diff --git a/src/screens/Game/screens/GameMainScreen.js b/src/screens/Game/screens/GameMainScreen.js index 23dd313..acdd4cd 100644 --- a/src/screens/Game/screens/GameMainScreen.js +++ b/src/screens/Game/screens/GameMainScreen.js @@ -67,6 +67,10 @@ class GameMainScreen extends React.Component { this.startGame(); } + componentWillUnmount() { + this.logic.stopGame(); + } + getRightButton = () => { return @@ -168,32 +172,6 @@ class GameMainScreen extends React.Component { }); } - showGameOverConfirm() { - let message = i18n.t("screens.game.gameOver.score") + this.state.gameScore + '\n'; - message += i18n.t("screens.game.gameOver.level") + this.state.gameLevel + '\n'; - message += i18n.t("screens.game.gameOver.time") + this.getFormattedTime(this.state.gameTime) + '\n'; - const onDismiss = () => { - this.onDialogDismiss(); - this.startGame(); - }; - this.setState({ - dialogVisible: true, - dialogTitle: i18n.t("screens.game.gameOver.text"), - dialogMessage: message, - dialogButtons: [ - { - title: i18n.t("screens.game.gameOver.exit"), - onPress: () => this.props.navigation.goBack() - }, - { - title: i18n.t("screens.game.resume"), - onPress: onDismiss - } - ], - onDialogDismiss: onDismiss, - }); - } - startGame = () => { this.logic.startGame(this.onTick, this.onClock, this.onGameEnd); this.setState({