2015-03-31 86 views
-4

雖然最近ZORK重新創建了一款遊戲,但我遇到了一個問題,不知道是什麼原因造成的,或者如何處理它。Javascript的限制或簡單的錯誤?

我在提示框上使用了一個while循環,比較用戶輸入和switch()情況,並返回一個響應。我一直在構建它一段時間,並有超過600行的JavaScript。

在爲遊戲的另一個元素添加了幾行後,我注意到腳本在使用過程中突然退出(這意味着提示不再顯示)。版本,我三重檢查錯誤,但找不到任何。我查閱了某些瀏覽器的JavaScript限制,並表示5,000,000是允許的最大語句。我認爲只有600條線路,甚至是循環線路,在使用一兩分鐘後就可以達到這個數量。

所以我很難過。我不知道是什麼導致了這個問題,或者如何解決或解決這個問題。我通過js minifier運行它,並得到與以前相同的結果。

//FNIJ V0.9 
 

 
function officeintro() { 
 
    alert('You are sitting in your office. There are two doors, one to the left and one to the right. They are both open.'); 
 
} 
 

 
function intro() { 
 
    alert('Welcome to Five Nights in Javascript!'); 
 
    alert('This game is a javascript recreation of Five Nights at Freddy\'s created by Scott Cawthon, and inspired by the game ZORK.\n\nPlease remember to not click \"Prevent this page from creating additional dialogs\". This will quit the game.'); 
 
    var gameconf = confirm('Are you familiar with Five Night at Freddy\'s? \n\nClick OK for yes, Cancel for no.'); 
 

 
    switch (gameconf) { 
 
     case false: 
 
      alert('Here is the description from Steam:\n\nWelcome to your new summer job at Freddy Fazbear\'s Pizza, where kids and parents alike come for entertainment and food as far as the eye can see! The main attraction is Freddy Fazbear, of course; and his two friends. They are animatronic robots, programmed to please the crowds! The robots\' behavior has become somewhat unpredictable at night however, and it was much cheaper to hire you as a security guard than to find a repairman.'); 
 

 
      alert('From your small office you must watch the security cameras carefully. You have a very limited amount of electricity that you\'re allowed to use per night (corporate budget cuts, you know). That means when you run out of power for the night- no more security doors and no more lights! If something isn\'t right- namely if Freddybear or his friends aren\'t in their proper places, you must find them on the monitors and protect yourself if needed!'); 
 
      alert('Five Nights at Freddys is better explained by either watching a gameplay video or playing the actual game.'); 
 
      alert('I recommend watching Pewdiepie or Markiplier (\my favorite)\ playing the game. Remember that this game is based off of the first game.'); 
 
      alert('The official game can be downloaded from "gg.gg/getfnaf", without the quotes.'); 
 
      break; 
 
     case true: 
 
      break; 
 
    } 
 

 

 
    var ctrlconf = confirm('Would you like to hear the controls? \n\nClick OK for yes, Cancel for no.'); 
 

 
    switch (ctrlconf) { 
 
     case true: 
 
      alert('You will be given a prompt box in which you can type commands.'); 
 
      alert('Most command will be intuitive, such as \'Open cam\', \'Close left door\', \'Check left light\' , etc.'); 
 
      alert('Some command can also be abbrevated. For example, \'Close cam\' can be abrivated to \'CC\'.'); 
 
      alert('You\'re all set! Let\'s get started!'); 
 
      officeintro(); 
 
      break; 
 
     case false: 
 
      alert('You\'re all set! Let\'s get started!'); 
 
      officeintro(); 
 
      break; 
 
    } 
 
} 
 

 

 
function gameplay() { 
 

 

 
    var stS = 1; 
 
    var stM = 1; 
 
    var stH = 1; 
 
    var time = 12; 
 
    var c = "Chica"; 
 
    var f = "Freddy"; 
 
    var b = "Bonnie"; 
 
    var cf = "Chica and Freddy"; 
 
    var fb = "Bonnie and Freddy"; 
 
    var cb = "Chica and Freddy"; 
 
    var cbf = "Bonnie, Chica and Freddy"; 
 
    var n = "Nothing"; 
 
    var showstage = cbf; 
 
    var backstage = n; 
 
    var supplycloset = n; 
 
    var diningarea = n; 
 
    var pirate = "Foxy"; 
 
    var whall = n; 
 
    var whallcorner = n; 
 
    var rr = n; 
 
    var kitchen = n; 
 
    var ehall = n; 
 
    var ehallcorner = n; 
 
    var lastcamsee = showstage; 
 

 
    var llight = n; 
 
    var rlight = n; 
 
    var goldfred = false; 
 
    var leftdoor = "open"; 
 
    var rightdoor = "open"; 
 
    var cam = "closed"; 
 
    var lastcam = "showstage"; 
 
    var look = "your office"; 
 
    var playing = true; 
 
    var night = 1; 
 
    var pwruse = 1; 
 
    var power = 99; 
 
    var pwrrate = 0; 
 
    var disppwr = 99; 
 

 
    function initTime() { 
 
     var now = new Date(); 
 
     stS = now.getSeconds(); 
 
     stM = now.getMinutes(); 
 
     stH = now.getHours(); 
 
    } 
 

 
    function computeTime() { 
 
     var now = new Date(); 
 
     var reS = now.getSeconds(); 
 
     var reM = now.getMinutes(); 
 
     var reH = now.getHours(); 
 
     var elapS = reS - stS; 
 
     var elapM = reM - stM; 
 
     var elapH = reH - stH; 
 
     if (elapM < 0) { 
 
      reM = reM + 60; 
 
      elapM = reM - stM; 
 
     } 
 
     if (elapS < 0) { 
 
      reS = reS + 60; 
 
      elapS = reS - stS; 
 
     } 
 
     var finalH = elapH * 3600; 
 
     var finalM = elapM * 60; 
 
     var finalS = finalM + elapS + finalH; 
 
     if (finalS > 86 && finalS < 172) { 
 
      time = 1; 
 
     } 
 
     if (finalS > 172 && finalS < 258) { 
 
      time = 2; 
 
     } 
 
     if (finalS > 258 && finalS < 344) { 
 
      time = 3; 
 
     } 
 
     if (finalS > 344 && finalS < 430) { 
 
      time = 4; 
 
     } 
 
     if (finalS > 430 && finalS < 516) { 
 
      time = 5; 
 
     } 
 
     if (finalS > 516) { 
 
      time = 6; 
 
      alert('5 A.M.'); 
 
      alert('A clock is chiming...'); 
 
      alert('6 A.M.'); 
 
      var advn = confirm('Start next night?\n\nOK for yes, Cancel for no.'); 
 
      switch (advn) { 
 
       case true: 
 
        night++; 
 
        initTime(); 
 
        setVars(); 
 
        break; 
 
       default: 
 
        playing = false; 
 
        alert('Thanks for playing! Goodbye!'); 
 
      } 
 

 
     } 
 
    } 
 

 

 
    function initPwr() { 
 
     var nowpwr = new Date(); 
 
     stpS = nowpwr.getSeconds(); 
 
     stpM = nowpwr.getMinutes(); 
 
     stpH = nowpwr.getHours(); 
 
    } 
 

 
    function computePwr() { 
 
     var nowpwr = new Date(); 
 
     var repS = nowpwr.getSeconds(); 
 
     var repM = nowpwr.getMinutes(); 
 
     var repH = nowpwr.getHours(); 
 
     var elappS = repS - stpS; 
 
     var elappM = repM - stpM; 
 
     var elappH = repH - stpH; 
 
     if (elappM < 0) { 
 
      repM = repM + 60; 
 
      elappM = repM - stpM; 
 
     } 
 
     if (elappS < 0) { 
 
      repS = reppS + 60; 
 
      elappS = repS - stpS; 
 
     } 
 
     var finalpH = elappH * 3600; 
 
     var finalpM = elappM * 60; 
 
     var finalpS = finalpM + elappS + finalpH; 
 
     if (pwruse == 1) { 
 
      pwrrate = .141; 
 
     } 
 
     if (pwruse == 2) { 
 
      pwrrate = .235; 
 
     } 
 
     if (pwruse == 3) { 
 
      pwrrate = .341; 
 
     } 
 
     if (pwruse == 4) { 
 
      pwrrate = .447; 
 
     } 
 
     var pwrsxr = finalpS * pwrrate; 
 
     power = power - pwrsxr; 
 
     disppwr = Math.floor(power); 
 
    } 
 

 

 
    function stopCall() { 
 
     document.getElementById('call').pause(); 
 
    } 
 

 
    function playcall() { 
 
     document.getElementById('call').play(); 
 
    } 
 

 
    function curiousAlert() { 
 
     alert('Error.\n\n How did you even get here?.\n Please email [email protected] with an explanation.'); 
 
    } 
 

 
    initTime(); 
 
    while (playing) { 
 
     computeTime(); 
 
     initPwr(); 
 
     var combox = prompt('Night: ' + night + ' \nTime: ' + time + ' A.M.\nPower Left: ' + disppwr + '%\nUsage: Lvl ' + pwruse + '\n\nGive a command'); 
 
     combox.toLowerCase(); 
 
     switch (combox) { 
 

 
      case "help": 
 
       alert('Help: \n\nFor a list of commands, type \'Com\' or \'Commands\'.\n\nIf you press the CANCEL button on the prompt box at any time, the game will be terminated.\n\nDue to the limitations of JavaScript, character movements are not functional. I am working towards a solution, so this will be fixed in a future update. '); 
 
       alert('UPDATE: \n\nI have figured out a workaround for the time mechanism!\n The time will work now!'); 
 
       break; 
 

 
      case "time": 
 
       alert('The time is ' + time + ' A.M.'); 
 
       break; 
 
      case "open door": 
 
       alert('Open which door?'); 
 
       break; 
 
      case "status": 
 
      case "stat": 
 
      case "stats": 
 
       alert('Status:\n\Time: ' + time + ' A.M.\nNight: ' + night + '\nPower Left: '+disppwr+' % \nPower usage: ' + pwruse + '.\nCamera is ' + cam + '.\nLeft door: ' + leftdoor + '. \nRight door: ' + rightdoor + '. \nYou are looking at ' + look + '.\nThe last viewed camera: ' + lastcam + '.'); 
 
       break; 
 

 
      case "com": 
 
      case "commands": 
 
      case "command": 
 
       alert('Here is a complete list of commands\n\nAny multiple word phrases can be used by abbreviating them \n\nHelp: Calls up the help menu \nCom: Shows this menu\nExit: Quits the game\nOpen Cam: Opens the security camera\nClose Cam: Closes the Security camera\nLook: Check what is in front of you\nOpen & Close Left/Right door: Self explanatory\nStatus: Show the status of the doors, time, power usage, etc.\nCams: Displays a complete list of camera names.'); 
 
       break; 
 

 
      case "cams": 
 
      case "cam": 
 
       alert('Here is a list of camera angles. Use them by typing the full name or abreviating them.\n\nShowstage\nBackstage\nRestrooms\nPirate Cove\nDining area\nKitchen\nSupply closet\nWest Hall\nWest Hall corner\nEast Hall\nEast Hall corner\n'); 
 
       break; 
 

 
      case "open cam": 
 
      case "open camera": 
 
      case "oc": 
 
       switch (cam) { 
 
        case "open": 
 
         alert('Your camera is already open'); 
 
         break; 
 
        default: 
 
         pwruse++; 
 
         cam = "open"; 
 
         look = "the camera"; 
 
         alert('Camera is open. You are looking at ' + lastcam + '. ' + lastcamsee + ' is here.'); 
 
       } 
 
       break; 
 

 
      case "left light": 
 
      case "ll": 
 
      case "check left light": 
 
      case "check ll": 
 
      case "cll": 
 
       //pwruse++; 
 
       alert('Checked the left light. ' + llight + ' appears.'); 
 
       break; 
 

 
      case "right light": 
 
      case "rl": 
 
      case "check right light": 
 
      case "check rl": 
 
      case "crl": 
 
       //pwruse++; 
 
       alert('Checked the right light. ' + rlight + ' appears.'); 
 
       break; 
 

 
      case "power use": 
 
      case "pu": 
 
      case "power usage": 
 
       alert('Power usage is at level ' + pwruse + '.'); 
 
       break; 
 

 
      case "answer phone": 
 
      case "phone call": 
 
      case "answer call": 
 
       alert('You found an easter egg!'); 
 
       var callplay = true; 
 
       playcall(); 
 
       var askcall = confirm('Do you hear the phone call?\n\nOK for yes, Cancel for no.'); 
 
       switch (askcall) { 
 
        case true: 
 
         alert('Back to the game then!'); 
 
         break; 
 
        default: 
 
         alert('Then it should load when the game is over.'); 
 
       } 
 
       alert('To stop the audio, type "mute".'); 
 
       break; 
 
      case "mute": 
 
       stopCall(); 
 
       break; 
 
      case "close cam": 
 
      case "close camera": 
 
      case "cc": 
 
       switch(cam) { 
 
       case "open": 
 
       switch (goldfred) { 
 
        case true: 
 
         cam = "closed"; 
 
         look = "golden freddy"; 
 
         alert('Camera is closed. You are looking at Golden Freddy.'); 
 
         alert(' '); 
 
         alert('IT\'S ME'); 
 
         alert(' '); 
 
         playing = false; 
 
         var i = 5; 
 
         while (i <= 0) { 
 
          console.log(i); 
 
          i++; 
 
         } 
 
         break; 
 

 
        case false: 
 
         cam = "closed"; 
 
         pwruse--; 
 
         look = "your office"; 
 
         alert('Camera is closed. You are looking at your office. The left door is ' + leftdoor + '. The right door is ' + rightdoor + '.'); 
 
         break; 
 
       } break; 
 
case "closed": alert('Your camera is already closed'); break;} 
 
       break; 
 

 
      case "look": 
 
       switch (look) { 
 
        case "your office": 
 
         alert('You are looking at ' + look + '. The left door is ' + leftdoor + ' and the right door is ' + rightdoor + '.'); 
 
         break; 
 

 
        case "the camera": 
 
         alert('You are look at ' + lastcam + '.'); 
 
       } 
 
       break; 
 

 
      case "old": 
 
      case "open left door": 
 
      case "open ld": 
 
       switch (cam) { 
 
        case "open": 
 
         alert('You cannot do that. Your camera is open.'); 
 
         break; 
 
        case "closed": 
 
         switch (leftdoor) { 
 
          case "open": 
 
           alert('The door is already open'); 
 
           break; 
 
          case "closed": 
 
           leftdoor = "open"; 
 
           pwruse--; 
 
           alert('Left door open'); 
 
           break; 
 
         } 
 
         break; 
 
        default: 
 
         curiousAlert(); 
 
       } 
 
       break; 
 

 
      case "find golden freddy": 
 
       switch (look) { 
 
        case "the camera": 
 
         goldfred = true; 
 
         alert('You are looking at west hall corner. The poster of freddy has been painted gold.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "cld": 
 
      case "close left door": 
 
      case "close ld": 
 
       switch (cam) { 
 
        case "open": 
 
         alert('You cannot do that. Your camera is open.'); 
 
         break; 
 
        case "closed": 
 
         switch (leftdoor) { 
 
          case "closed": 
 
           alert('The left door is already closed'); 
 
           break; 
 
          case "open": 
 
           pwruse++; 
 
           leftdoor = "closed"; 
 
           alert('Left door closed'); 
 
           break; 
 
         } 
 
         break; 
 
        default: 
 
         curiousAlert(); 
 
       } 
 
       break; 
 

 
      case "ord": 
 
      case "open right door": 
 
      case "open rd": 
 
       switch (cam) { 
 
        case "open": 
 
         alert('You cannot do that. Your camera is open.'); 
 
         break; 
 
        case "closed": 
 
         switch (rightdoor) { 
 
          case "open": 
 
           alert('The right door is already open'); 
 
           break; 
 
          case "closed": 
 
           rightdoor = "open"; 
 
           pwruse--; 
 
           alert('Right door open'); 
 
           break; 
 
         } 
 
         break; 
 
        default: 
 
         curiousAlert(); 
 
       } 
 
       break; 
 

 
      case "crd": 
 
      case "close right door": 
 
      case "close rd": 
 
       switch (cam) { 
 
        case "open": 
 
         alert('You cannot do that. Your camera is open.'); 
 
         break; 
 
        case "closed": 
 
         switch (rightdoor) { 
 
          case "closed": 
 
           alert('The right door is already closed'); 
 
           break; 
 
          case "open": 
 
           pwruse++; 
 
           rightdoor = "closed"; 
 
           alert('Right door closed'); 
 
           break; 
 
         } 
 
         break; 
 
        default: 
 
         curiousAlert(); 
 
       } 
 
       break; 
 

 
      case "showstage": 
 
      case "ss": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "the showstage"; 
 
         lastcamsee = showstage; 
 
         alert('You are looking at the showstage. ' + showstage + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 
      case "dining area": 
 
      case "da": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "dining area"; 
 
         lastcamsee = diningarea; 
 
         alert('You are looking at the dining area. ' + diningarea + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "backstage": 
 
      case "bs": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "backstage"; 
 
         lastcamsee = backstage; 
 
         alert('You are looking backstage. ' + backstage + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "pirate cove": 
 
      case "pc": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "pirate cove"; 
 
         lastcamsee = pirate; 
 
         alert('You are looking at pirate cove. ' + pirate + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "restrooms": 
 
      case "restroom": 
 
      case "rr": 
 
      case "rest rooms": 
 
      case "rest room": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcamsee = rr; 
 
         lastcam = "restroom"; 
 
         alert('You are looking at the rest rooms. ' + rr + ' is here.'); 
 
       } 
 
       break; 
 

 
      case "supply closet": 
 
      case "sc": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "supply closet"; 
 
         lastcamsee = supplycloset; 
 
         alert('You are looking at the supply closet. ' + supplycloset + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "kitchen": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "kitchen"; 
 
         lastcamsee = kitchen; 
 
         alert('The camera is disabled. You are listening to the kitchen. You hear ' + kitchen + '.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "ehc": 
 
      case "east hall corner": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "east hall corner"; 
 
         lastcamsee = ehallcorner; 
 
         alert('You are looking at the east hall corner. ' + ehallcorner + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "eh": 
 
      case "east hall": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "east hall"; 
 
         lastcamsee = ehall; 
 
         alert('You are looking at the east hall. ' + ehall + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "whc": 
 
      case "west hall corner": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "west hall corner"; 
 
         lastcamsee = whallcorner; 
 
         alert('You are looking at the west hall corner. ' + whallcorner + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 
      case "wh": 
 
      case "west hall": 
 
       switch (look) { 
 
        case "the camera": 
 
         lastcam = "west hall"; 
 
         lastcamsee = whall; 
 
         alert('You are looking at the west hall. ' + whall + ' is here.'); 
 
         break; 
 
        default: 
 
         alert('Please open your camera first'); 
 
       } 
 
       break; 
 

 

 
      case "exit": 
 
       var close = confirm('Do you really want to quit?'); 
 
       switch (close) { 
 
        case true: 
 
         alert('Thanks for playing! Goodbye!'); 
 
         playing = 0; 
 
         break; 
 
        case false: 
 
         break; 
 
        default: 
 
         curiousAlert(); 
 
       } 
 
       break; 
 

 
      case false: 
 
       alert('Please enter something. If you are trying to quit the game, type EXIT.'); 
 
       break; 
 

 
      default: 
 
       alert('I didn\'t understand that. Use "com" for a complete list of commands or "help" for more help.'); 
 

 
     } //End of switch statement 
 
     computePwr(); 
 
    } //End of while loop 
 
} //End of Gameplay Function 
 

 
intro(); 
 
gameplay();
<audio id="call" src="https://89d0b33f6a3e9119d631e45a4b55916d65742a63.googledrive.com/host/0B-uT9QTKkBoRM2pONTNHSHZiZmM/call.mp3" preload="auto"></audio>

+1

我懷疑你應該做四重檢查。 – Pointy 2015-03-31 15:23:05

+0

然後在這裏貼出實際的代碼,儘管有「多少行」。問題會很快結束,否則。 – 2015-03-31 15:23:44

+1

也許控制檯出現錯誤。 – epascarello 2015-03-31 15:24:14

回答

1

你真的應該學會使用控制檯在瀏覽器中。我只是打它與控制檯打開一點點,並得到了一個錯誤:

Uncaught ReferenceError: reppS is not defined 

在你computePwr功能,您有一個名爲repS變量,但一次鍵入它作爲reppS

repS = reppS + 60; 

更正:

repS = repS + 60; 

下一次,你應該檢查你的控制檯。你也可以通過jslint來運行你的代碼,它會捕獲簡單的拼寫錯誤和其他錯誤。

+0

感謝您的幫助。我無法使用控制檯,因爲我在iPad上做了這件事。 – 2015-03-31 15:36:33

+0

你仍然可以使用jslint。請參閱我答案中的鏈接。 – forgivenson 2015-03-31 15:37:57

+0

將在未來使用。我一直在使用http://Jshint.com/,但這樣好多了 – 2015-03-31 15:40:24