+2

How do I restart/reset the code in Javascript?

I am making a game .In that game there are 3 lives totally.I I want to restart/reset the whole code when lives are 0.(using HTML/CSS/JS for game)

8/25/2019 8:18:51 PM

Aziz Mandıracı

6 Answers

New Answer

+3

declare the variables such as score and live at global scope (outside any function) without initializing. define a function named init to initialize these global variables. at game end, prompt gameover modal box and display a button for restart. the restart button is listening to click event, with init as the callback. https://code.sololearn.com/WrZat1vLX669/?ref=app

+2

Fuzzy Squid thx for answer.

+1

So to reset the code, I assume you mean to go back to the beginning, and you can do it with a simple function. All you have to do is to put your code in a function. Then just call your function every times your lives hit 0. function Game { if (lives == 0) Game(); } If your code is really long, or if writing if-statements all day isn't your thing, put it inside a function. function isDead(health) { if (health == 0) Game(); } function Game() { lives -= 1; isDead(lives); } ---------------------------------------------------------------------------------------------------------- Well if you're like me and use C++, the you can use goto loop. It's one of the most helpful loops in C++. I'm not sure if there's a similar loop in JS, but if there is then use it. Start: if (lives == 0) goto Start;

0

Well, HTML and CSS barely changes, and it's not that difficult to reset values in JS

0

@Aziz No problem

0

This is actually a question concerning that topic...would breaking an if statement work?