[英]I have no idea: x is undefined
我正在制作一个四合一游戏,我几乎完成了,但是在检查是否有人获胜时出现了这个烦人的错误:
Uncaught TypeError: d[(y + 1)] is undefined
我不知道是什么导致了问题以及它发生在哪里,这是我的代码:
let rows = 10
let cols = 10
let size = 50
let padding = "5px"
let turn = 1
let win = 0
let tileDiv = document.getElementById("tiles")
const data = [];
for (let i = 0; i < rows; i++) {
data.push((new Array(cols)).fill(0));
}
function createTiles() {
let a = 0
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
a = document.createElement("img")
a.src = "tiles/0.png"
a.height = size
a.style = "padding: " + padding
a.id = String(y) + "_" + String(x)
tileDiv.appendChild(a)
tileDiv.lastChild.addEventListener("click", function () { clickedTile(this) })
}
a = document.createElement("p")
tileDiv.appendChild(a)
}
}
function setTile(x, y, z) {
let a = data[y]
a[x] = z
data[y] = a
document.getElementById(String(y) + "_" + String(x)).src = "tiles/" + z + ".png"
}
function gravity(x, z) {
let i = 0
for (let i = 0; i < rows; i++) {
if ((data[i])[x] == 0) {
y = i
}
}
if (data[y][x] == 0) {
setTile(x, y, z)
}
else { alert("Column full, next player") }
}
function checkVerticalWinFor(x, y, d) {
console.log("checkV-" + x + "-" + y)
let a = d[y][x]
let b = d[(y - 1)][x]
let c = d[(y - 2)][x]
let d_ = d[(y - 3)][x]
if (a == b && b == c && c == d_ && d_ == a) {
win = a
if (win != 0) {
setTile(x, y, (win + "_win"))
setTile(x, y - 1, (win + "_win"))
setTile(x, y - 2, (win + "_win"))
setTile(x, y - 3, (win + "_win"))
}
}
}
function checkHorizontalWinFor(x, y, d) {
console.log("checkH-" + x + "-" + y)
let a = d[y][x]
let b = d[y][(x + 1)]
let c = d[y][(x + 2)]
let d_ = d[y][(x + 3)]
if (a == b && b == c && c == d_ && d_ == a) {
win = a
if (win != 0) {
setTile(x, y, (win + "_win"))
setTile(x + 1, y, (win + "_win"))
setTile(x + 2, y, (win + "_win"))
setTile(x + 3, y, (win + "_win"))
}
}
}
function checkDiagonalWinFor(x, y, d) {
console.log("checkD-" + x + "-" + y)
let a = d[y][x]
let b = d[(y + 1)][(x + 1)]
let c = d[(y + 2)][(x + 2)]
let d_ = d[(y + 3)][(x + 3)]
if (a == b && b == c && c == d_ && d_ == a) {
win = a
if (win != 0) {
setTile(x, y, (win + "_win"))
setTile(x + 1, y + 1, (win + "_win"))
setTile(x + 2, y + 2, (win + "_win"))
setTile(x + 3, y + 3, (win + "_win"))
}
}
}
function checkDiagonal_WinFor(x, y) {
console.log("checkD_-" + x + "-" + y)
let a = d[y][x]
let b = d[(y + 1)][(x - 1)]
let c = d[(y + 2)][(x - 2)]
let d_ = d[(y + 3)][(x - 3)]
if (a == b && b == c && c == d_ && d_ == a) {
win = a
if (win != 0) {
setTile(x, y, (win + "_win"))
setTile(x - 1, y + 1, (win + "_win"))
setTile(x - 2, y + 2, (win + "_win"))
setTile(x - 3, y + 3, (win + "_win"))
}
}
}
function checkWin() {
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
if (data[y][x] != 0) {
checkVerticalWinFor(x, y, data)
if (win == 0) {
checkHorizontalWinFor(x, y, data)
if (win == 0) {
checkDiagonalWinFor(x, y, data)
if (win == 0) { checkDiagonal_WinFor(x, y, data) }
}
}
}
}
}
}
function clickedTile(z) {
if (win == 0) {
q = z.id
c = q.split("_")
x = c[1]
gravity(x, turn)
if (turn == 1) { turn = 2 }
else if (turn == 2) { turn = 1 }
checkWin()
}
}
createTiles()
我知道这很多,但有人可以帮忙吗? 错误表明问题出在第 98 行。
setTile 函数中的文件位置只是图像格式的图块:-0.png -1.png -1_win.png -2.png -2_win.png
看到2件事:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.