The onGameOver()
function is already defined in frontend/src/Components/View/index.js
.
This function is triggered automatically when the game ends.
Locate the function, which looks like this.
const onGameOver = async (data) => {
setScore(data.detail.score);
if (dataManager.name === "") {
setTimeout(() => {
setShowNameDialog(true);
setGameView(GAME_VIEW.MAIN_MENU);
}, 3000);
} else {
await dataManager.submitScore(data.detail.score);
setTimeout(() => {
setGameView(GAME_VIEW.MAIN_MENU);
setShowLeaderboard(true);
}, 3000);
}
}
Extract the conditional part of onGameOver()
into a separate startScoreSubmission()
function, and then modify the results to match the following.
const onGameOver = async (data) => {
setScore(data.detail.score);
}
const startScoreSubmission = async () => {
if (dataManager.name === "") {
setShowNameDialog(true);
setGameView(GAME_VIEW.MAIN_MENU);
} else {
await dataManager.submitScore(score);
setGameView(GAME_VIEW.MAIN_MENU);
setShowLeaderboard(true);
}
}
Notice these important differences in the new startScoreSubmission()
function.
-
The setTimeout
handlers have been removed.
You no longer need a delay before displaying the Game Over screen and moving to score submission.
That transition is now triggered by user action.
-
The argument you’re passing to submitScore()
has changed from data.detail.score
to just score
.
Originally, submitScore()
was called in onGameOver()
, so it could get the score from the data object that was passed as an argument to onGameOver()
.
But since startScoreSubmission()
doesn’t accept any arguments, submitScore()
now gets the score from the variable that’s set by the state hook.
Lastly, update onGameOver()
to behave appropriately based on whether there’s a price.
const onGameOver = async (data) => {
setScore(data.detail.score);
const price = Number(Koji.remix.get().price);
if (price > 0) {
setShowPaymentDialog(true);
emitEvent('pauseGame');
} else {
startScoreSubmission();
}
}
This code first gets the price from remixData
and makes sure it’s a number.
If there is a price, it shows PaymentDialog
.
Otherwise, it goes straight to score submission and the leaderboard.