在 p5.js javascript class、"Script error. (: line 0)" 中使用 getter 和 setter 的问题
Issue using getters and setters in p5.js javascript class, "Script error. (: line 0)"
我创建了一个 class,它接收矢量坐标数组,然后在这些坐标所在的位置绘制叶子草图。我正在写一个 getter 和一个 setter,您可以在其中更改这些叶子的颜色。叶子颜色从 minHue 和 maxHue 中肆虐。我在 运行 和 class 时收到 "Script error. (: line 0)" 但无法找到问题所在。
我已经查看了所有大小写错误的代码,但无法完成任何错误。
class drawLeaves {
constructor(leafArray) {
this.leafs = leafArray;
this.randomColor = true;
this.minHue = 0;
this.maxHue = 0;
}
genLeaves(minDiam, maxDiam, minAlpha, maxAlpha) {
if (this.randomColor) {
var rdn0 = random(255);
var rdn1 = random(255);
this.minHue = min(rdn0, rdn1);
this.maxHue = max(rdn0, rdn1);
} else {
var colors = this.leafColor;
minHue = colors[0];
maxHue = colors[1];
}
let i;
for (i = 0; i < this.leafs.length; i++) {
let h = map(i, 0, this.leafs.length, this.minHue, this.maxHue);
let s = 255;
let b = 255;
let a = random(minAlpha, maxAlpha);
fill(h, s, b, a);
let diam = random(minDiam, maxDiam);
let jitterX = random(-30, 30);
let jitterY = random(-30, 30);
ellipse(this.leafs[i].x + jitterX, this.leafs[i].y + jitterY, diam, diam);
}
}
draw() {
this.genLeaves(0, 90, 0, 0.03); // big leaves
this.genLeaves(0, 15, 0, 0.25); // small leaves
}
set leafColor(minHue, maxHue) {
this.minHue = minHue;
this.maxHue = maxHue;
this.randomColor = false;
}
get leafColor() {
return [this.minHue, this.maxHue]
}
}
正在寻求反馈以帮助消除错误,非常感谢。
当我在 Babel REPL 中 运行 时,出现以下错误:
repl: setter should have exactly one param (48:2)
根据文档 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set#Description),setter 应该只有 1 个参数。
尝试重构,这样您就可以只有 1 个参数或将其更改为:
setLeafColor(minHue, maxHue) {
code
}
我创建了一个 class,它接收矢量坐标数组,然后在这些坐标所在的位置绘制叶子草图。我正在写一个 getter 和一个 setter,您可以在其中更改这些叶子的颜色。叶子颜色从 minHue 和 maxHue 中肆虐。我在 运行 和 class 时收到 "Script error. (: line 0)" 但无法找到问题所在。
我已经查看了所有大小写错误的代码,但无法完成任何错误。
class drawLeaves {
constructor(leafArray) {
this.leafs = leafArray;
this.randomColor = true;
this.minHue = 0;
this.maxHue = 0;
}
genLeaves(minDiam, maxDiam, minAlpha, maxAlpha) {
if (this.randomColor) {
var rdn0 = random(255);
var rdn1 = random(255);
this.minHue = min(rdn0, rdn1);
this.maxHue = max(rdn0, rdn1);
} else {
var colors = this.leafColor;
minHue = colors[0];
maxHue = colors[1];
}
let i;
for (i = 0; i < this.leafs.length; i++) {
let h = map(i, 0, this.leafs.length, this.minHue, this.maxHue);
let s = 255;
let b = 255;
let a = random(minAlpha, maxAlpha);
fill(h, s, b, a);
let diam = random(minDiam, maxDiam);
let jitterX = random(-30, 30);
let jitterY = random(-30, 30);
ellipse(this.leafs[i].x + jitterX, this.leafs[i].y + jitterY, diam, diam);
}
}
draw() {
this.genLeaves(0, 90, 0, 0.03); // big leaves
this.genLeaves(0, 15, 0, 0.25); // small leaves
}
set leafColor(minHue, maxHue) {
this.minHue = minHue;
this.maxHue = maxHue;
this.randomColor = false;
}
get leafColor() {
return [this.minHue, this.maxHue]
}
}
正在寻求反馈以帮助消除错误,非常感谢。
当我在 Babel REPL 中 运行 时,出现以下错误:
repl: setter should have exactly one param (48:2)
根据文档 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set#Description),setter 应该只有 1 个参数。
尝试重构,这样您就可以只有 1 个参数或将其更改为:
setLeafColor(minHue, maxHue) {
code
}