我认为这个简单的代码中的语法没有任何问题。但只要我在generator.display();
函数中调用draw
,就会弹出一条错误消息:“脚本错误。(:第0行)”。
你可以在这里查看并运行代码:https://editor.p5js.org/huskyspeaks/sketches/-dN7ZQ9pg
您可能会发现(假设在线编辑器没有任何问题),删除generator.display();
将删除错误。但我真的不明白为什么会这样。我没有看到这个简单帧的编码方式有什么问题。
var generator;
function setup() {
createCanvas(400, 640);
generator = new Generator(width / 2, height / 2, 4);
}
function draw() {
background(55);
generator.display();
}
var Generator = function(x, y, m) {
this.pos = createVector(x, y);
this.mass = m;
this.display = function() {
ellipse(pos.x, pos.y, 10 * mass, 10 * mass);
}
}
如果代码确实有问题,我怎么能更新它?
你缺少对this
的引用。
ellipse(this.pos.x, this.pos.y, 10 * this.mass, 10 * this.mass);
你在pos
上创建了mass
和this
但是在没有它的情况下引用它。将其更改为如上所示将其修复。