简体   繁体   English

Javascript:为什么警报说未定义而不是字母“k”

[英]Javascript: why is the alert saying undefined and not the letter "k"

I can get my array to work and display perfectly as a 2d array but the second I attempt to add another element it never shows it always stays undefined.我可以让我的数组工作并完美地显示为二维数组,但第二次我尝试添加另一个元素,它从未显示它始终保持未定义状态。 I tried many different methods, in this version I attempted to duplicate the way that I add the 2d elements.我尝试了许多不同的方法,在这个版本中,我尝试复制添加 2d 元素的方式。

Ultimately I want to save data to the array to make the walls "#" that are displaying correctly to have further data stating "solid" and then I would be able to test for that before moving the "@"最终,我想将数据保存到数组中,以使正确显示的墙壁“#”具有进一步说明“固体”的数据,然后我将能够在移动“@”之前对其进行测试

I actually have a working version of this but using a second array which would get very cumbersome if I add further data.我实际上有一个工作版本,但使用第二个数组,如果我添加更多数据,这将变得非常麻烦。

Also as it is right now I am surprised the entire map is not getting overwritten with the letter k同样现在我很惊讶整个地图没有被字母 k 覆盖

 function gameloop(){ var mainArray = []; var mapSizeX = 32; var mapSizeY = 128; var arrayDepth = 10; var idPos = {x:0, y:0}; function initMap(mainArray, mapSizeX, mapSizeY){ for (var i = 0; i < mapSizeX; i++) { mainArray.push([0]) for (var j = 0; j < mapSizeY; j++) { mainArray[i][j] = "."; if(j == 0){mainArray[i][j] = "#";} if(j == mapSizeY-1){mainArray[i][j] = "#";} if(i == 0){mainArray[i][j] = "#";} if(i == mapSizeX-1){mainArray[i][j] = "#";} for (var k = 0; k < arrayDepth; k++) { mainArray[i][j][k] = "k"; } } } } function nl(){GameScreen.innerText += "\\n";} function render() {GameScreen.innerText = mainArray.map(arr => arr.join("")).join("\\n"); nl(); nl();} function reposition(xChange,yChange,strA){ //mainArray[idPos.x][idPos.y] = "."; //idPos.x = idPos.x + xChange; //idPos.y = idPos.y + yChange; //mainArray[idPos.x][idPos.y] = "@"; if(mainArray[idPos.x+xChange][idPos.y+yChange][1] === "Solid"){GameLog.innerText ="You can not travel in that direction"} else{ mainArray[idPos.x][idPos.y] = "."; idPos.x = idPos.x + xChange; idPos.y = idPos.y + yChange; mainArray[idPos.x][idPos.y] = "@"; GameLog.innerText = "You take a step to the " + strA alert(mainArray[0][0][1]); } render(); } //Startup initMap(mainArray, mapSizeX, mapSizeY); idPos.x = mapSizeX/2; idPos.y = mapSizeY/2; mainArray[idPos.x][idPos.y] = "@"; //First Render render(); document.addEventListener('keydown', function(event) { if(event.keyCode === 38 ){reposition(-1,0,"North");} if(event.keyCode === 40 ){reposition(1,0,"South");} if(event.keyCode === 37 ){reposition(0,-1,"West");} if(event.keyCode === 39 ){reposition(0,1,"East");} //alert(event.keyCode); }); } gameloop();
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello</title> </head> <body> <br> <p style="color:#7d7d7d;font-family:Lucida Console;">Dungeon Valley.<br> <font style="color:#ABABAB;font-family:Lucida Console;font-size:0.5em";> Taming the Borderlands.<br> v0.005 By heromedel. </P></font> <P> <section id="GameScreen" style="color:#000000;font-family:Lucida Console;"></section> <P> <section id="GameLog" style="color:#000000;font-family:Lucida Console;">Arrow Keys to move.<br></section> <script src="main.js"></script> </body> </html>

If I'm understanding what you're trying to do, this is wrong:如果我理解你想要做什么,这是错误的:

for (var k = 0; k < arrayDepth; k++) {
   mainArray[i][j][k] = "k";
}

mainArray is supposed to be a 2d array of strings, right? mainArray应该是一个二维字符串数组,对吗? So why are you using var k to add a 3rd dimension to it?那么为什么要使用var k为其添加第三维? Unless you're trying to append to the string?除非你想追加到字符串? In Javascript you append to strings with + , not with [] .在 Javascript 中,您使用+附加到字符串,而不是使用[]

By the way, this is also wrong:顺便说一句,这也是错误的:

mainArray.push([0])

You want to push an empty array [] , not an array with a 0 in it [0] .您想推送一个空数组[] ,而不是其中包含 0 的数组[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM