简体   繁体   中英

How to restart JS game?

I am currently coding a JS game. After the game ends, I want the player to be able to click the "go back to menu" button, be directed to the home page, and then be able to click play and start a new game. The problem I have is that I'm not sure how to reload the game after clicking play. When I click play again, it brings me back to the game over screen with the final score of the previous play.

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();
  }
};

//TEXT mic.
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);
  }
};

That's because all your variables are declared in the "global" scope (in the beginning of the JS), so calling any method like game() or menu() just makes these methods reuse the old values from these variables.

I would suggest you wrap the whole code you provided as an example in a "class" (making the variables and the methods - properties of the class). In this way, when a button is clicked, you could easily destroy the instance of the class and recreate a new class in its place (resetting all local variables inside the class instance, causing all previous progress to be erased).

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM