我正在尝试使用此代码显示我将拥有的级别...
levelArray[0] = ["player", "empty", "empty", "empty", "wall", "wall", "empty",
"empty", "wall", "wall", "empty", "empty", "empty","empty",
"empty", "empty", "empty", "wall", "empty", "wall", "wall",
"wall", "wall", "empty", "wall", "empty", "wall", "empty",
"box", "wall", "empty", "wall", "empty", "empty", "empty",
"empty", "empty", "empty", "wall", "wall", "wall", "empty",
"wall", "wall", "empty", "empty", "ghost", "wall","ghost"];
for(var i = 0; i < edge; i++)
{
for(var j = 0; j < edge; j++)
{
switch(levelArray[i])
{
case "empty": // empty location
ctx.drawImage(emptyTile, currentX, currentY);
break;
case "wall": // wall block
ctx.drawImage(wallTile, currentX, currentY);
break;
case "box": // box block
ctx.drawImage(boxTile, currentX, currentY);
break;
case "ghost": // enemy sprite
ctx.drawImage(ghostTile, currentX, currentY);
break;
case "player": // player sprite
ctx.drawImage(playerTile, currentX, currentY);
break;
}
currentX += elementEdge;
}
currentY += elementEdge;
}
但是我在切换行“未捕获的类型错误:无法读取未定义的属性 '0'”上收到错误,我真的不明白。
编辑:edge 以前是这样定义的
edge = Math.sqrt(levelArray.length)