在p5.js javascript类中出现了使用getter和setter的问题,“脚本错误。(:第0行)”

时间:2019-01-09 13:17:49

标签: javascript class setter getter p5.js

我创建了一个类,它接收矢量坐标数组,然后草绘这些坐标所在的叶子。我正在写一个吸气剂和二传手,您可以在其中更改这些叶子的颜色。叶子的颜色从minHue和maxHue激增。运行类时出现“脚本错误。(:第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]
  }
}

正在寻找反馈来帮助消除错误,非常感谢。

1 个答案:

答案 0 :(得分:0)

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
}