【问题标题】:Cannot invoke disableStyle() on the array type PShape[]无法在数组类型 PSShape[] 上调用 disableStyle()
【发布时间】:2022-01-19 23:44:39
【问题描述】:

我的处理代码有问题。 我做了一个网格女巫,在随机位置填充了一些 SVG。使用 PNG 和 PImage 效果很好,但我想使用 SVG 来更改图标的描边颜色、重量和大小。

在我将代码写入 Shape 以使其与 SVG 一起工作后,我想放置一个 disableStyle() 来更改值,但我收到错误“无法在数组类型 PShape[] 上调用 disableStyle()”。

任何人建议问题是什么?

希望得到一些好的答案。谢谢!

int countHorizontal = 12;
int countVertical = 6;
int seed = 0;

PShape[] Icon = new PShape[8];

void setup() {
  size(1600, 800);
  noLoop();
  randomSeed(seed);
  
  Icon[0] = loadShape("SVGIcons/Icon_1.svg"); Icon[1] = loadShape("SVGIcons/Icon_2.svg");
  Icon[2] = loadShape("SVGIcons/Icon_3.svg"); Icon[3] = loadShape("SVGIcons/Icon_4.svg");
  Icon[4] = loadShape("SVGIcons/Icon_5.svg"); Icon[5] = loadShape("SVGIcons/Icon_6.svg");
  Icon[6] = loadShape("SVGIcons/Icon_7.svg"); Icon[7] = loadShape("SVGIcons/Icon_8.svg");
}

void draw() {
  background(#eeeeee);
  
  Icon.disableStyle();
  
  strokeWeight(2);
  color(#000000);

  
  for (int i = 0; i < countHorizontal; i = i + 1) {
    for (int j = 0; j < countVertical; j = j + 1) {

      float w = float(width) / countHorizontal;
      float h = float(height) / countVertical;
      float x = w * i;
      float y = h * j;

      shape(Icon[(int)random(Icon.length)], x, y, 100, 100); 
    }
  }
}

void keyReleased() { 
  if (keyCode == LEFT) {
    seed = seed - 1;
  }

  if (keyCode == RIGHT) {
    seed = seed + 1;
  }
  
  randomSeed(seed);
  redraw();

  println(seed);

  //saveFrame("export/animation_####.png");
}

【问题讨论】:

    标签: processing


    【解决方案1】:

    您的Icon 变量是PShape[] 类型。方括号表示它是一个array 的 PShape 对象。 Array 没有名为 disableStyle() 的方法,这就是为什么您会收到无法在 Icon 变量上调用该方法的错误。

    要使用该方法,您需要访问数组中的各个 PShape 元素:

    Icon[0].disableStyle(); // etc
    

    【讨论】:

    • 谢谢,它工作得很好。喜欢 StackOverflow 的工作原理!
    • 我可以只为一个图标指定特定的宽度和高度吗?除了宽度是两倍之外,它们的尺寸都相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多