Fixed game tick interval continuing after game exit

This commit is contained in:
Arnaud Vergnet 2020-07-21 22:45:45 +02:00
parent 8864e686bd
commit 9f4a8c837d
2 changed files with 9 additions and 28 deletions

View file

@ -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);
}

View file

@ -67,6 +67,10 @@ class GameMainScreen extends React.Component<Props, State> {
this.startGame();
}
componentWillUnmount() {
this.logic.stopGame();
}
getRightButton = () => {
return <MaterialHeaderButtons>
<Item title="pause" iconName="pause" onPress={this.togglePause}/>
@ -168,32 +172,6 @@ class GameMainScreen extends React.Component<Props, State> {
});
}
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({