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

编程入门 行业动态 更新时间:2024-10-10 10:26:07
本文介绍了Javascript:为什么警报说未定义而不是字母"k"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以让我的数组工作并完美地显示为2d数组,但第二个尝试添加另一个从未显示过的元素,该元素永远不会保持未定义状态.我尝试了许多不同的方法,在此版本中,我尝试复制添加2d元素的方式.

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.

最终,我想将数据保存到数组中,以使正确显示的墙#"具有进一步的数据,表明实心",然后在移动"@"之前可以进行测试

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.

现在也很奇怪,整个地图没有被字母k覆盖

Also as it is right now I am surprised the entire map is not getting overwritten with the letter 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 应该是二维的字符串数组,对吗?那么,为什么要使用 var k 向其中添加第3维?除非您尝试附加到字符串?在Javascript中,您使用 + 附加字符串,而不使用 [] .

mainArray is supposed to be a 2d array of strings, right? So why are you using var k to add a 3rd dimension to it? Unless you're trying to append to the string? In Javascript you append to strings with +, not with [].

顺便说一句,这也是错误的:

By the way, this is also wrong:

mainArray.push([0])

您要推送一个空数组 [] ,而不是其中包含0的数组 [0] .

You want to push an empty array [], not an array with a 0 in it [0].

更多推荐

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

本文发布于:2023-11-28 00:04:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   字母   而不是   未定义   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!