-1
我正在嘗試使用JavaScript和[p5] [1]製作迷宮生成器。每次我運行的代碼,雖然它說:無法讀取未定義的屬性'xPos'(JavaScript)
遺漏的類型錯誤:在抽獎(sketch.js:24)無法讀取的不確定 財產「XPOS」 ......
並且還,那隻能說明我的無聊電網。我一直試圖解決這個問題,我找不到解決方案。這裏是我的代碼:
Sketch.js:
var cellSize = 32;
var cols = 20;
var rows = 20;
var grid = create2DArray(cols, rows);
var stack = [];
var currentCell = grid[0][0];
function setup() {
createCanvas(cols * cellSize, rows * cellSize);
generateCells();
}
function draw() {
background(230);
for (var x = 0; x < cols; x++) {
for (var y = 0; y < rows; y++) {
grid[x][y].draw();
}
}
randomNeighbor(currentCell.xPos/cellSize, currentCell.y/cellSize);
}
function create2DArray(c, r) {
var arr = new Array(c);
for (var i = 0; i < c; i++) {
arr[i] = new Array(r);
}
return arr;
}
function generateCells() {
for (var x = 0; x < cols; x++) {
for (var y = 0; y < rows; y++) {
var cell = new Cell(x * cellSize, y * cellSize);
grid[x][y] = cell;
}
}
}
function randomCell() {
var randomX = floor(random(0, cols));
var randomY = floor(random(0, rows));
var cell = grid[randomX][randomY];
grid[randomX][randomY].visited = true;
return cell;
}
function randomNeighbor(x, y) {
if (getMoveable(x, y)) {
append(stack, currentCell);
var neighbors = [];
if (x - 1 >= 0) append(neighbors, grid[x-1][y]);
if (x + 1 <= cols) append(neighbors, grid[x+1][y]);
if (y - 1 >= 0) append(neighbors, grid[x][y - 1]);
if (y + 1 <= rows) append(neighbors, grid[x][y+1]);
var rand = floor(random(0, neighbors.length));
currentCell = neighbors[rand];
removeWall(currentCell, stack[stack.length]);
}
else {
currentCell = stack.pop();
}
}
function getMoveable(x, y) {
var moveable = false;
var neighbors = [];
if (x - 1 >= 0) append(neighbors, grid[x-1][y]);
if (x + 1 <= cols) append(neighbors, grid[x+1][y]);
if (y - 1 >= 0) append(neighbors, grid[x][y - 1]);
if (y + 1 <= rows) append(neighbors, grid[x][y+1]);
for (var i = 0; i < neighbors.length; i++) {
if (neighbors[i].visited == true) {
moveable = true;
break;
}
}
return moveable;
}
function removeWall(a, b) {
if (a.xPos - b.xPos == -cellSize) {
grid[a.xPos/cellSize][a.yPos/cellSize].right = false;
}
else if (a.xPos - b.xPos == cellSize) {
grid[b.xPos/cellSize][b.yPos/cellSize].right = false;
}
else if (a.yPos - b.yPos == -cellSize) {
grid[a.xPos/cellSize][a.yPos/cellSize].bottom = false;
}
else if (a.yPos - b.yPos == cellSize) {
grid[b.xPos/cellSize][b.yPos/cellSize].bottom = false;
}
}
Cell.js:
function Cell(xx, yy) {
this.xPos = xx;
this.yPos = yy;
this.visited = false;
this.right = true;
this.bottom = true;
this.draw = function() {
stroke(0);
strokeWeight(1);
if (this.right) {
line(this.xPos + cellSize, this.yPos, this.xPos + cellSize, this.yPos + cellSize);
}
if (this.bottom) {
line(this.xPos, this.yPos + cellSize, this.xPos + cellSize, this.yPos + cellSize);
}
}
}
index.html的(事情我在我的瀏覽器中打開):
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.sound.min.js"></script>
<script src="cell.js"></script>
<script src="sketch.js"></script>
<style> body {padding: 0; margin: 0;} </style>
</head>
<body>
</body>
</html>
,修復了錯誤,但現在有一個新的:遺漏的類型錯誤:無法讀取性能在getMoveable(草圖的不確定 「參觀」您填寫的
grid
與細胞您應該儘快更新currentCell
。 js:81) – Xephyr我會盡力解決這個問題... – Xephyr
@NumNumDude在將一個元素附加到'neighbors'數組之前,你必須檢查它是否存在。 –