I work on a game and my goal is this: when the game is over the user can restart the game with a mouse click.
But the code $(document).click(startFunction); acts like startFunction();.
I also noticed that if $(document).click is added to the stage preceding the "game over" stage, then the first $(document).click calls the next function directly and the second $(document).click works like it supposed to.
What am I missing here? Thanks in advance.
Here is my JS code. It has been simplified with only the "shell" related to my questions remain intact.
dislayedGameDescription();
function gameOver() {
$(document).off("click");
// more code...
$(document).click(moveByComputer);
}
function gameStart() {
$(document).off("click");
// more code...
moveByComputer();
}
function moveByComputer() {
$(document).off("click");
// more code...
$(".btn").click(moveByUser);
}
function moveByUser() {
$(".btn").off("click");
// more code...
determination();
}
function determination() {
if (1) {
if (2) {
moveByComputer();
} else {
$(".btn").click(moveByUser);
}
} else {
// I want to place here gameOver(); but it works only if I place
$(document).click(gameOver);
// And it acts like gameOver(); here so the next $(document).click in the gameOver function acts normal
}
}
function dislayedGameDescription() {
// more code...
$(document).on("click", gameStart);
}