Javascript的限制或简单错误

Limitations of Javascript or simple error?

本文关键字:简单 错误 Javascript      更新时间:2023-09-26

最近在以ZORK的风格重新创建游戏时,我遇到了一个问题,不知道是什么导致了它,也不知道如何处理它。

我在提示框上使用了while循环,将用户输入与switch()情况进行比较,并返回响应。我已经在上面构建了一段时间,现在有600多行JavaScript。

在为游戏的另一个元素添加了几行之后,我注意到脚本在使用过程中突然退出(这意味着提示不再显示。)以前的任何版本都从未出现过这种情况,我反复检查了错误,但找不到任何错误。我查看了某些浏览器上的JavaScript限制,它说5000000是允许的最大语句数。我不认为只有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 charles.fecht@outlook.com 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 "answerswer phone":
            case "phone call":
            case "answerswer 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>

您真的应该学会在浏览器中使用控制台。我只是在控制台打开的情况下玩了一会儿,结果出现了一个错误:

Uncaught ReferenceError: reppS is not defined

computePwr函数中,您有一个名为repS的变量,但有一次您将其键入为reppS

repS = reppS + 60;

更正:

repS = repS + 60;

下次,您应该检查您的控制台。您也可以通过jslint运行您的代码,它会发现简单的打字错误和其他错误。