JavaScript中的game over函数错误,并返回函数



在第55行我试图返回函数gameover。但是这个函数有问题,我们的想法是在用户死亡时从头开始重新启动脚本。函数从第3行开始,在第70行结束,我的参数可能有问题吗?还是别的什么?

(function(blink) {
   gameover = function() {
  confirm(["You are stranded in the woods, you see a red blinking light off",
  "in the distance you want to pursue it but their are creatures of the unknown out there",
  "do you wish to continue?"].join('n'));
  var forward = prompt("Does the user wish to continue? 'y' is  yes and 'n' is no");
  if (forward == "y") {
    console.log("Welcome to the game!");
  } else {
    console.log("Sorry this game is not for you :( ");
  }
  console.log (["With hopes of escaping you run towards the light",
  "but the light fades as you get closer, you seem pretty exhausted from the search.",
  "Your eyes begin to get heavier by the minute. You see a rock with some moss on it",
  "lying besides the dirt path"].join('n'));
  var sleep = prompt ("Do you wish to try and get some rest and continue the search tomorrow? 'y' is  yes and 'n' is no");
  if (sleep == "y") {
    console.log("You decided to try and get some rest and woke up feeling replishined in the morning ");
  } else {
    console.log(["You decide to keep on with your journey and try to keep yourself awake.",
    "You keep telling yourself, 'if I can just find the light their might be people waitnig",
    "for me there"].join('n'));
  }
  console.log("Suddenly you feel a cold metal hand grasp your ankle, you try and run but their is just know use. if only you could find some type of object to pry yourself free.")
  var freedom = prompt()
  if (freedom == "r" ) {
    console.log("You look to the right, there's nothing there but dirt.");
  } else if (freedom == "l") {
      console.log("You look to the left and find a rusty pry bar, by your side");
  } else {
    console.log("Try using the 'l' arrow key to look left and the 'r' arrow key to to look right'..");
  }
  var escape = prompt("You think to yourself i could take this robot if want or i could just escape without the risk..");
  if(escape == "f") {
    console.log(["You Try and fight the meatl creature by hitting it on the top of its",
    "head it stuns the robot long enough for you to get your ankle loose",
    "you see an compartment full of wires and fuses. You hae enough",
    "time to get away while the robot is stunned, but on the otherhand",
    "you think about how safe would feel in the woods with out the robot there"].join('n'));
  } else if ("r") {
    console.log(["You try and pry the metal hand off your ankle.",
    "but you upsest the beast to only discover its a robot the robot then takes you",
    "and chews you into tiny pieces only to consumed by his metal gullet"].join('n'));
    return gameover()
  } else {
     console.log("Try using the 'f' arrow key to fight the robot or you can run by using the 'r' arrow key");
   }
   var shutdown = prompt("Try and shutdown the robot or try and escape with the fear of knowing its still out there");
   if (shutdown == "s") {
     console.log(["You jam the pry bar into the robots fuse box watching sparks",
   "and flames shoot out in every direction, feeling a sense of relief you decide",
   "to carry the pry bar with you as it might come in handy on the journey."].join('n'));
 } else if ("e") {
    console.log("Lorem ipsum you've escaped");
} else {
  console.log("Try using the 's' key to shutdown the robot or try using the 'e' key to escape");
}
   gameover (console.log"Game Over!");
})(blink);

这段代码不对:

gameover (console.log"Game Over!");

你应该写:

console.log("Game Over!");
gameover();

…但更大的问题是,当gameover仍在处理时,你会再次调用gameover。这意味着一堆调用正在建立,当第二个游戏结束时,处理将继续在第一个中进行。

相反,一个更好的方法是在整个"游戏"周围有一个处理程序,当你想继续时可以重新运行....

像这样:

1)将"gameover"功能改为"playGame"

2)把它放在playGame函数之后,而不是里面(它会调用playGame()函数)

wantToPlay = true;
while (wantToPlay) {
  wantToPlay = playGame();
}
// Only gets here if playGame() returns "false"

3)当你想完成游戏时,做:

console.log("You are dead, game is over");
return false;

嘿,你可能想尝试使用谷歌Chrome调试它?例如,在Chrome ->点击ctrl+shift+j,看看你是否可以识别任何语法错误,或者你可以设置断点。

https://developer.chrome.com/devtools/docs/javascript-debugging

最新更新