我现在在深水中。不确定我知道我在做什么!无论如何,我正在尝试为我的蛇游戏绘制地图。目前,我正在尝试添加墙,但无法正常工作!这是代码: 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]。显示不是函数(草图:第52行)”
我不知道从哪里开始。我正在尝试很多事情,但是就像我在黑暗中拍摄一样。有人知道怎么了吗? /简
答案 0 :(得分:4)
您仅初始化数组中1
的位置,但是您的代码期望整个二维数组包含Wall
的实例。
在不了解自己在做什么的情况下,您需要检查每个对象是否都是wall的实例
if (walls[i][j] instanceof Wall) {
walls[i][j].display();
}
答案 1 :(得分:0)
问题是您试图从walls
数组中获取墙,该数组也包含零(命名错误)。根据您的设计,不能保证第一面墙位于[0][0]
您应该只为墙壁创建一个单独的数组:
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();
}
}
或者,您应该先遍历所有项目,然后检查每个项目是否都是一堵墙,然后再继续:
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();
}
}
}
答案 2 :(得分:0)
这不是解决问题的方法,但是您仍然应该做一些事情。重构此功能
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 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);
}
在您的原始代码中,对Wall
构造函数的每次调用都会创建一个新的独立(尽管彼此相同)display
函数,这在性能上是很糟糕的。请改用函数原型。