因此,我将使用JavaScript创建基于文本的RPG,并在创建名称并在程序冻结的提示后输入y。有人可以帮我吗?问题似乎出在我确认名称并将其更改为另一个数字并将settingTheName变量传递给刚刚冻结的uiPrompt函数时。我不知道为什么会这样。

<!doctype html>
<html>
<head>
<title>Random RPG</title>
</head>
<body>
<p id="text"></p>
<input id="prompt"/>
<button id='butoon' onclick="uiPrompt(options,functions)">Enter</button>
<script>

var settingTheName = 0;

//used for ui to determine in certain prompts which commands are acceptable
var options;
//the input that the player puts in the ui box
var playerInput;
var functions= {
        newGame: function() {
                document.getElementById('text').innerHTML = 'Welcome to the world of Aldania, a magical and mystical world where anything can happen! Aldania is filled with adventure so grab a sword, some Mountain Dew, Doritos, and click the link below to download some free ram! Just Kidding! We are going to create a new character for you! Lets start with his name.';
            settingTheName = 1;
        },
        loadGame: function() {
                document.getElementById('text').innerHTML = 'Welcome to the world of Aldania, a magical and mystical world where anything can happen! Aldania is filled with adventure so grab a sword, some Mountain Dew, Doritos, and click the link below to download some free ram! Just kidding! Just create a fucking character aoadBSIHBIYCBSIY the game.';
        },
        setName: function() {
            playerInput = document.getElementById("prompt").value;
            document.getElementById('text').innerHTML = 'Are you sure you want your character to be named ' +playerInput+ '? Press Y for yes and N for No.';
            options= {
                createTheChar:  ['y', 'yes'],
                newGame: ['n', 'no']
            };
        },
        creeteTheChar: function() {
            for (var j=0; j < 7; ++j ) {
                if (j=0) {
                    document.getElementById('text').innerHTML = 'Do you prefer to channel your power with your faith in God or the Devil?';
                } else if (j=1) {
                    document.getElementById('text').innerHTML = 'Whats your favorite element? Fire, Cold, Lightning, or Poison?';
                } else if (j=2) {
                    document.getElementById('text').innerHTML = 'Are you more of a guy who is very defensive or more ferocious?';
                } else if (j=3) {
                    document.getElementById('text').innerHTML = 'Would you like to run around yelling incantations and throwing magic missiles or unleash the power of the mind upon your foes?';
                } else if (j=4) {
                    document.getElementById('text').innerHTML = 'When keeping undercover, do you use the shadows to your advantage or speedily sneak around?';
                } else if (j=5) {
                    document.getElementById('text').innerHTML = 'Are you the happy go lucky type of guy or do you prefer to meticulously calculate your plans?';
                } else if (j=6) {
                    document.getElementById('text').innerHTML = 'Do you wanna play a super silly character?';

                }
            }
        }
};

//handles user input
function uiPrompt () {
    playerInput = document.getElementById("prompt").value;
    if (settingTheName == 1) {
        document.getElementById('text').innerHTML = 'Are you sure you want your character to be named ' +playerInput+ '? Press Y for yes and N for No.';
        options= {
            creeteTheChar: ['y', 'yes'],
            newGame: ['n', 'no']
        };
        settingTheName = 2;
    } else {
        for (functionName in options) {
            // check if playerInput is in options[functionName] array
            if (options[functionName].indexOf(playerInput) >= 0) {
                    functions[functionName]() // call function
                break
            }
        }
    }
}

//start menu at the beginning of the game
function startMenu () {
    options= {
        newGame:  ['n', 'new game', 'game'],
        loadGame: ['l', 'load game', 'game']
    };
    document.getElementById('text').innerHTML = 'RANDOM RPG: Where everything is random as f***! \n (N)ew Game \n (L)oad Game';
}

startMenu();
</script>
</body>
</html>

最佳答案

检查变量是否等于值时,您的'creeteTheChar'函数需要使用==而不是=。否则,您将每次设置该值。这将导致您的函数无限循环,因为j始终小于7。

您还希望在每次迭代之后而不是之前递增j。因此,++ j代替了j ++。如果您不这样做,则j == 0永远不会为真。另外,creeteTheChar函数在进入循环的下一个迭代之前不会等待任何用户输入。因此,它最终立即完成了循环,并且“您想扮演一个超级愚蠢的角色​​吗?”被陈列。

并不是为了阻止您,而是如果您打算有很多选择的话,您希望代码更具动态性。否则,您将得到大量无法管理和跟踪错误的过程代码。要正确执行此操作,您将需要使用类。

无论如何,这是creeteTheChar函数可以正确评估j的值:

    creeteTheChar: function() {
        console.log('creating character');
        for (var j=0; j < 7; j++ ) {
            if (j==0) {
                document.getElementById('text').innerHTML = 'Do you prefer to channel your power with your faith in God or the Devil?';
            } else if (j==1) {
                document.getElementById('text').innerHTML = 'Whats your favorite element? Fire, Cold, Lightning, or Poison?';
            } else if (j==2) {
                document.getElementById('text').innerHTML = 'Are you more of a guy who is very defensive or more ferocious?';
            } else if (j==3) {
                document.getElementById('text').innerHTML = 'Would you like to run around yelling incantations and throwing magic missiles or unleash the power of the mind upon your foes?';
            } else if (j==4) {
                document.getElementById('text').innerHTML = 'When keeping undercover, do you use the shadows to your advantage or speedily sneak around?';
            } else if (j==5) {
                document.getElementById('text').innerHTML = 'Are you the happy go lucky type of guy or do you prefer to meticulously calculate your plans?';
            } else if (j==6) {
                document.getElementById('text').innerHTML = 'Do you wanna play a super silly character?';

            }
        }
    }

10-06 04:50