I'm building a game to practice my skills. Everytime someone scores a point, the game starts again really fast and I'd like it to give it a little time before going back. The main function looks like this:
function drawGame() {
background(); //does the background
drawChar();
drawEnemy();
drawBall();
changeMovement();
playerControls();
enemyFollowsBall();
enemyChangePosition();
checkBallCollision();
drawScore();
//if(pointScored()) {
//setInterval(drawGame, 1000);
//}
setTimeout(drawGame, 1);
}
The function keeps being called through setTimeOut, which manages the game animations. I tried to solve my problem with the commented out code (I set pointScore(), which increments the score and does some resetting, to return true), but it didn't work as I expected. How can I obtain the expected effect?
You would still call setTimeout(drawGame, 1); when you dont move it into the else statement or add a return after.
function drawGame() {
background(); //does the background
drawChar();
drawEnemy();
drawBall();
changeMovement();
playerControls();
enemyFollowsBall();
enemyChangePosition();
checkBallCollision();
drawScore();
if (pointScored()) {
setInterval(drawGame, 5000);
return; // either this
} else { // or adding the else here
setTimeout(drawGame, 1);
}
}