简体   繁体   English

Function 未被识别为 function?

[英]Function not recognized as a function?

I'm in deep water at the moment.我现在处于深水中。 Not sure I know what I'm doing, Anyway.不知道我知道我在做什么,反正。 I'm trying to make a map for my snake game: At the moment I'm trying to add a wall but it wont work!我正在尝试为我的蛇游戏制作 map: 目前我正在尝试添加一堵墙,但它不会工作! This is the code: https://editor.p5js.org/JensHaglof/sketches/YwtUO8992这是代码: https://editor.p5js.org/JensHaglof/sketches/YwtUO8992

Or written out:或写出:

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();
  }
  }

}

I get an error "TypeError: walls[i][j].display is not a function (sketch: line 52)"我收到错误“TypeError:walls[i][j].display is not a function(草图:第 52 行)”

I have no clue where to begin.我不知道从哪里开始。 I'm trying so many things but it's like I'm shooting in the dark.我尝试了很多事情,但就像我在黑暗中拍摄一样。 Anyone knows what's wrong?有谁知道出了什么问题? /Jens /詹斯

You are only initializing the spots where 1 s are in your array, but your code expects the entire 2D array to contain instances of Wall .您只是在初始化数组中1的位置,但您的代码期望整个 2D 数组包含Wall的实例。

Without understanding what you are doing, you need to check whether each object is an instance of wall不了解你在做什么,你需要检查每个 object 是否是墙的实例

if (walls[i][j] instanceof Wall) {
  walls[i][j].display();
}

The problem is that you are trying to get the walls from the walls array which also contains zeros (bad naming).问题是您试图从还包含零的walls数组中获取墙壁(命名错误)。 Based on your design, the first wall is not guaranteed to be at [0][0]根据您的设计,不保证第一面墙位于[0][0]

You should either create a separate array just for the walls:您应该为墙壁创建一个单独的数组:

var wallList = [];
var walls = [];

function setup() {
  createCanvas(400, 400);

  for (let i = 0; i < walls.length; i++) {
    for (let j = 0; j < walls.length; j++) {
      if (walls[i][j] == 1) {
        walls[i][j] = new Wall(walls[i] * 20, walls[j] * 20);
        wallList.push(walls[i][j]);
      }
    }
  }
}

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 < wallList.length; i++) {
    wallList[i][j].display();
  }
}

Or you should check loop through all the items and check if each one is a wall before continuing:或者你应该检查所有项目的循环,并在继续之前检查每个项目是否是一堵墙:

var walls = [];

function setup() {
  createCanvas(400, 400);

  for (let i = 0; i < walls.length; i++) {
    for (let j = 0; j < walls.length; j++) {
      if (walls[i][j] == 1) {
        walls[i][j] = new Wall(walls[i] * 20, walls[j] * 20);
      }
    }
  }
}

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 < walls.length; i++) {
    for (let j = 0; j < walls.length; j++) {
      if(walls[i][j] instanceof Wall) walls[i][j].display();
    }
  }
}

Not a solution to your problem, but something you should do nevertheless.不是解决您的问题的方法,而是您应该做的事情。 Refactor this function重构这个 function

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);
  }
}

into进入

function Wall(x, y){
  this.x = x;
  this.y = y;
  rect(this.x, this.y, 20, 20);
}

Wall.prototype.display = function() {
  fill(255, 255, 0);
  rect(this.x, this.y, 20, 20);
}

In your original code, avery call of the Wall constructor will create a new independent (even though identical to every other one) display function, which performance-wise is just bad.在您的原始代码中, Wall构造函数的每次调用都会创建一个新的独立(即使与其他所有) display function,这在性能方面很糟糕。 Use the function prototype instead.请改用 function 原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM