gpt4 book ai didi

Javascript 程序在按下某个按键时卡住。为什么?

转载 作者:行者123 更新时间:2023-12-03 10:16:59 27 4
gpt4 key购买 nike

因此,我正在使用 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 函数在进入循环中的下一次迭代之前不会等待任何用户输入。所以它最终会立即结束循环,然后“你想扮演一个 super 愚蠢的 Angular 色​​吗?”显示。

并不是要阻止您,但如果您打算有很多选择,您会希望代码更加动态。否则,您最终会得到堆积如山的程序代码,无法管理和跟踪错误。为了正确地做到这一点,您需要使用类。

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

    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?';

}
}
}

关于Javascript 程序在按下某个按键时卡住。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com