我现在正处于深水中。不确定我知道我在做什么!无论如何,我正在尝试为我的贪吃蛇游戏制作一张 map 。目前我正在尝试添加一堵墙,但它不会工作!这是代码: https://editor.p5js.org/JensHaglof/sketches/YwtUO8992
或者写出来:
wallCount = 0
var walls = []
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
walls.push([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 20; i++){
for (let j = 0 ; j < 20 ; j++){
if (walls[i][j] == 1){
walls[i][j] = new Wall(walls[i]*20, walls[j]*20);
wallCount = wallCount + 1
}
}
}
}
function Wall(x, y){
this.x = x;
this.y = y;
rect(this.x, this.y, 20, 20);
this.display = function(){
fill(255, 255, 0);
rect(this.x, this.y, 20, 20);
}
}
function draw() {
background(0);
for (let i = 0 ; i < wallCount ; i++){
for (let j = 0 ; j < wallCount ; j++){
walls[i][j].display();
}
}
}
我得到一个错误“TypeError: walls[i][j].display is not a function (sketch: line 52)”
我不知道从哪里开始。我正在尝试很多事情,但这就像我在黑暗中拍摄。任何人都知道怎么了? /延斯
最佳答案
您只是初始化数组中 1
所在的位置,但您的代码期望整个二维数组包含 Wall
的实例。
在不理解你在做什么的情况下,你需要检查每个对象是否是墙的实例
if (walls[i][j] instanceof Wall) {
walls[i][j].display();
}
关于javascript - 功能未被识别为功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061715/