读取数组javascript的内容时出现问题
因此,我有一个基于文本的游戏,我会提示用户输入一些文本,并根据给定的选项执行特定的功能。但是,当代码读取数组的长度时,表示数组未定义。代码如下:读取数组javascript的内容时出现问题,javascript,arrays,Javascript,Arrays,因此,我有一个基于文本的游戏,我会提示用户输入一些文本,并根据给定的选项执行特定的功能。但是,当代码读取数组的长度时,表示数组未定义。代码如下: //handles user input function uiPrompt (options) { for (var i = 0; i < options[0].length; ++i) { if (playerInput = options[i][0]) {break; } options[i][0];
//handles user input
function uiPrompt (options) {
for (var i = 0; i < options[0].length; ++i) {
if (playerInput = options[i][0]) {break; }
options[i][0];
}
}
//start menu at the beginning of the game
function startMenu () {
options = [ ['n', 'l', 'new game', 'load game', 'new', 'load'],
['newGame()', 'loadGame()', 'newGame()', 'loadGame()', 'newGame()', 'loadGame()'] ];
document.getElementById('text').innerHTML = 'RANDOM RPG: Where everything is random! \n (N)ew Game \n (L)oad Game';
uiPrompt();
}
//处理用户输入
功能提示(选项){
对于(变量i=0;i<选项[0]。长度;++i){
如果(playerInput=options[i][0]){break;}
期权[i][0];
}
}
//游戏开始时的开始菜单
功能开始菜单(){
选项=['n','l','new game','load game','new','load'],
['newGame()'、'loadGame()'、'newGame()'、'loadGame()'、'newGame()'、'loadGame()'];
document.getElementById('text').innerHTML='RANDOM RPG:Where everything is RANDOM!\n(n)ew Game\n(L)oad Game';
uiPrompt();
}
因此,在options变量中,第一行是用户输入的字符串,代码将接受这些字符串作为有效的播放器选择。第一行中每个选项元素正下方的相邻元素是输入相应选项时要执行的函数。例如,如果玩家要键入“n”以开始一个新游戏,则将执行其正下方的元素newGame()函数。我的问题是,当按下“提交用户输入”按钮(调用uiPrompt函数)时,我收到错误,Uncaught TypeError:无法读取此行未定义的属性“0”:
for (var i = 0; i < options[0].length; ++i) {
for(变量i=0;i
我做错了什么?提前thanx。在我看来,您调用uiPrompt时没有使用选项参数 尝试:
要调用函数调用时,不能将函数调用作为字符串存储在数组中。 (好吧,你可以,但那是伊芙) 也许做点像
var options: {
newGame: ['n', 'new game', 'game'],
loadGame: ['l', 'load game', 'game']
}
var functions: {
newGame: function() {
/* your function */
},
loadGame: function() {
/* your function */
}
}
那么在uiPromt你可以做什么
for (functionName in options) {
// check if playerInput is in options[functionName] array
if (options[functionName].indexOf(playerInput) >= 0) {
functions[functionName]() // call function
break
}
}
刚刚更新了你的js,你在初始化时出现了一些错误,所以我更正了它们。只需将值记录在uiPromt中,这就是你想要实现的吗
//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;
//handles user input
function uiPrompt () {
playerInput = document.getElementById("prompt").value;
for (var i = 0; i < options.length; ++i) {
if (playerInput == options[i][0]) {break; }
console.log(options[i][0]);
}
}
//start menu at the beginning of the game
function startMenu () {
options = [ ['n', 'l', 'new game', 'load game', 'new', 'load'],
['newGame()', 'loadGame()', 'newGame()', 'loadGame()', 'newGame()', 'loadGame()'] ];
document.getElementById('text').innerHTML = 'RANDOM RPG: Where everything is random! \n (N)ew Game \n (L)oad Game';
}
//the new game menu
function newGame () {
document.getElementById('text').innerHTML = 'You started a new game.';
}
startMenu();
//用于ui在某些提示中确定哪些命令是可接受的
var期权=[];
//播放器在ui框中输入的内容
var-playerInput;
//处理用户输入
函数提示(){
playerInput=document.getElementById(“提示符”).value;
对于(变量i=0;i
看起来您没有将选项数组传递到函数中,这使得uiPromt函数无法访问它。它应该是uiPromt(选项)。我希望您已经在某个地方为您分配了options变量,否则options将被设置为全局变量,以后可能会导致问题。我将options设置为全局变量,只是没有将其放在问题中。您仍在将名为options的空参数传递到参数中。通过调用uiP将数组传递到uiPromt函数中romt(选项)在你的startMenu函数中。这有意义吗?看看这个:,它应该更有意义。如果你在调用函数时不将参数传递到函数中,它将被视为未定义。我应该用什么替换functionName?另外,我计划在整个游戏中调用uiPrompt,并且不会总是只有两个函数可以调用选择要执行的选项。例如,我可能会在游戏中包含一个场景,在这个场景中,他们可以选择三个不同的选项来触发三个不同的函数,或者五个或二十个!您不需要替换它。它是一个变量,它将包含newGame
或loadGame
您能给我一个wh至少,整个脚本与您的修订版类似?您可以在任何函数中调用任意数量的函数,因此可以使用函数调用替换/*您的函数*/或者使用switch语句选择您的函数
//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;
//handles user input
function uiPrompt () {
playerInput = document.getElementById("prompt").value;
for (var i = 0; i < options.length; ++i) {
if (playerInput == options[i][0]) {break; }
console.log(options[i][0]);
}
}
//start menu at the beginning of the game
function startMenu () {
options = [ ['n', 'l', 'new game', 'load game', 'new', 'load'],
['newGame()', 'loadGame()', 'newGame()', 'loadGame()', 'newGame()', 'loadGame()'] ];
document.getElementById('text').innerHTML = 'RANDOM RPG: Where everything is random! \n (N)ew Game \n (L)oad Game';
}
//the new game menu
function newGame () {
document.getElementById('text').innerHTML = 'You started a new game.';
}
startMenu();