我在JavaScript中使用函数游戏并将其返回给函数时出错

时间:2015-07-27 23:57:28

标签: javascript function debugging return

在第55行,我正在尝试返回功能游戏。但是它们的功能有问题,我们的想法是在用户死亡时从头开始重新启动脚本。功能从第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);

2 个答案:

答案 0 :(得分:0)

此代码不对:

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

你应该写:

console.log("Game Over!");
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;

答案 1 :(得分:0)

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

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