javascript - 功能未被识别为功能?

标签 javascript constructor p5.js

我现在正处于深水中。不确定我知道我在做什么!无论如何,我正在尝试为我的贪吃蛇游戏制作一张 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/

相关文章:

javascript - 使用 api 检查文件是否在 Chrome 中下载

c++ - 以自定义类作为参数的构造函数,抛出 'No matching function for call to...'

c++ - 在 C++ 的菱形问题中,为什么我们需要从子类调用 grand_parent 构造函数?

javascript - 在 p5.js 中如何将光标替换为圆圈而不是将其绘制到 Canvas 上?

canvas - 有没有办法在 P5.js 中加载和剪辑带有圆形边框的图像?

javascript - 如何使用 html/javascript 在 Firefox 中禁用斜杠和撇号键弹出快速查找?

javascript - 在 View.event Backbone.js 中创建一个新模型

javascript - 如何将数据从mysql数据库发送到JS脚本?

javascript - 使用 Acrobat Javascript API 获取选定的文本

python - 在 __init__ 中构造对象