声明为原型的函数出现时未定义
Function declared as prototype comes up as undefined
我有这段代码,其中有一个粒子对象和一个数组来保存它的所有实例。然后,我试图创建一个函数来调用每个实例的 draw() 和 update() 函数。:
var particles;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
particles = [];
for (var i = 0; i < 5; i++) {
particles.push(1);
}
};
function particle() {
this.x = random(0, width);
this.y = random(0, height);
};
particle.prototype.draw = function() {
noStroke();
fill(255, 0, 0);
rect(this.x, this.y, 20, 20);
};
particle.prototype.update = function() {
this.x+=random(-0.1, 0.1);
this.y+=random(-0.1, 0.1);
};
Array.prototype.run = function() {
for (var i = 0; i < this.length; i++) {
this[i].update();
this[i].draw();
}
};
function draw() {
background(255);
particles.run();
};
这是我的错误消息:this[i].update is not a function
。如果我记录函数,它是 "undefined"。这与我声明为原型有关系吗?我也知道修改 Array 对象通常是不好的做法,但到目前为止我还不知道有其他方法可以创建可以直接在数组上使用的函数。除了 p5 库之外,我没有使用任何库或内容。对此最 efficient/clean 的解决方案是什么?
您的 setup()
函数正在创建一个数字数组,而不是 particle
数组。应该是:
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
particles = [];
for (var i = 0; i < 5; i++) {
particles.push(new particle);
}
};
我假设您实际上希望粒子是粒子的数组
注1:将run
添加到particles
数组的代码只
注 2:将 particle
更改为 Particle
- 仅出于 "convention" 原因
var particles;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
particles = [];
for (var i = 0; i < 5; i++) {
particles.push(new Particle);
}
};
function Particle() {
this.x = random(0, width);
this.y = random(0, height);
};
Particle.prototype.draw = function() {
noStroke();
fill(255, 0, 0);
rect(this.x, this.y, 20, 20);
};
Particle.prototype.update = function() {
this.x+=random(-0.1, 0.1);
this.y+=random(-0.1, 0.1);
};
// add run to particles, not to Array.prototype
Object.defineProperty(particles, 'run', {
value: function() {
for (var i = 0; i < this.length; i++) {
this[i].update();
this[i].draw();
}
}
});
function draw() {
background(255);
particles.run();
};
我有这段代码,其中有一个粒子对象和一个数组来保存它的所有实例。然后,我试图创建一个函数来调用每个实例的 draw() 和 update() 函数。:
var particles;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
particles = [];
for (var i = 0; i < 5; i++) {
particles.push(1);
}
};
function particle() {
this.x = random(0, width);
this.y = random(0, height);
};
particle.prototype.draw = function() {
noStroke();
fill(255, 0, 0);
rect(this.x, this.y, 20, 20);
};
particle.prototype.update = function() {
this.x+=random(-0.1, 0.1);
this.y+=random(-0.1, 0.1);
};
Array.prototype.run = function() {
for (var i = 0; i < this.length; i++) {
this[i].update();
this[i].draw();
}
};
function draw() {
background(255);
particles.run();
};
这是我的错误消息:this[i].update is not a function
。如果我记录函数,它是 "undefined"。这与我声明为原型有关系吗?我也知道修改 Array 对象通常是不好的做法,但到目前为止我还不知道有其他方法可以创建可以直接在数组上使用的函数。除了 p5 库之外,我没有使用任何库或内容。对此最 efficient/clean 的解决方案是什么?
您的 setup()
函数正在创建一个数字数组,而不是 particle
数组。应该是:
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
particles = [];
for (var i = 0; i < 5; i++) {
particles.push(new particle);
}
};
我假设您实际上希望粒子是粒子的数组
注1:将run
添加到particles
数组的代码只
注 2:将 particle
更改为 Particle
- 仅出于 "convention" 原因
var particles;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
particles = [];
for (var i = 0; i < 5; i++) {
particles.push(new Particle);
}
};
function Particle() {
this.x = random(0, width);
this.y = random(0, height);
};
Particle.prototype.draw = function() {
noStroke();
fill(255, 0, 0);
rect(this.x, this.y, 20, 20);
};
Particle.prototype.update = function() {
this.x+=random(-0.1, 0.1);
this.y+=random(-0.1, 0.1);
};
// add run to particles, not to Array.prototype
Object.defineProperty(particles, 'run', {
value: function() {
for (var i = 0; i < this.length; i++) {
this[i].update();
this[i].draw();
}
}
});
function draw() {
background(255);
particles.run();
};