I'm currently working on a JavaScript game project. Once the game is finished, I'd like to give players the option to go back to the main menu by clicking a button, then start a new game by clicking play again. However, I've run into an issue where reloading the game after clicking play sends me back to the game over screen with the previous score displayed.
var keys = [];
var health = 100;
var score = 0;
var scene = "menu";
var clicked = false;
keyPressed = function() {
keys[keyCode] = true;
};
keyReleased = function() {
keys[keyCode] = false;
};
textFont(createFont("calibri", 35));
//Buttons
var button = function(cJ) {
this.x = cJ.x;
this.y = cJ.y;
this.w = cJ.w;
this.h = cJ.h;
};
button.prototype.mouseIsOver = function() {
return (
mouseX > this.x &&
mouseX < this.x + this.w &&
mouseY > this.y &&
mouseY < this.y + this.h
);
};
button.prototype.draw = function() {
noStroke();
textAlign(CENTER);
if (this.mouseIsOver() === true) {
fill(232, 232, 232);
rect(this.x + 3, this.y - 3, this.w, this.h);
} else if (this.mouseIsOver() === false) {
fill(255, 255, 255);
rect(this.x + 3, this.y - 5, this.w, this.h);
}
};
var playButton = new button({
x: width / 2 - 60,
y: height / 2 + 21,
w: 120,
h: 45
});
var howButton = new button({
x: width / 2 - 60,
y: 284,
w: 120,
h: 45
});
var backButton = new button({
x: width / 2 - 197,
y: 365,
w: 70,
h: 30
});
var restartButton = new button({
x: width / 2 - 102,
y: 250,
w: 200,
h: 36
});
var menuC = function() {
if (howButton.mouseIsOver()) {
how();
} else if (playButton.mouseIsOver()) {
game();
}
};
var howC = function() {
if (backButton.mouseIsOver()) {
menu();
}
};
var loseC = function() {
if (restartButton.mouseIsOver()) {
Program.restart();
}
};
mouseClicked = function() {
if (scene === "menu") {
menuC();
} else if (scene === "how") {
howC();
} else if (scene === "lose") {
loseC();
}
};
//Menu Text
draw = function() {
if (scene === "menu") {
menu();
//Title
textSize(47);
fill(41, 207, 182);
text("ACIDIC RAIN", 207, 147);
//Buttons
playButton.draw();
howButton.draw();
textSize(30);
fill(41, 207, 182);
text("PLAY", 202, 248);
text("HOW", 202, 311);
}
if (scene === "game") {
game();
}
if (scene === "how") {
how();
backButton.draw();
fill(41, 207, 182);
textSize(17);
text("BACK", 40, 380);
}
if (scene === "lose") {
lose();
restartButton.draw();
fill(0);
textSize(20);
text("GO BACK TO MENU", 201, 270);
}
};