所以我在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>
答案 0 :(得分:1)
当检查变量是否等于某个值时,你的'creeteTheChar'函数需要使用==而不是just =。否则,您每次都要设置值。这导致你的函数无限循环,因为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?';
}
}
}