0

谁能与我分享为什么我会收到此错误?基本上这是一个我想模拟基本植物生长的程序。我想这样做的方式是花瓣都存储在一个圆圈数组中。

Stem myStem;
Circle circles;

float scaleFactor=0.5;

void setup() {
  size(floor(400*scaleFactor), floor(800*scaleFactor));
  myStem = new Stem(200,800);

}

void draw() {

  background(150);
  smooth();
  Circle circles[];
  circles = new Circle[5];
  circles[0]  = new Circle(0, -40, 50, 50);
  circles[1]  = new Circle(0, -40, 50, 50);
  circles[2]  = new Circle(0, -40, 50, 50);
  circles[3]  = new Circle(0, -40, 50, 50);
  circles[4]  = new Circle(0, -40, 50, 50);

  for (int i = 0; i < circles.length; i++) {
   circles = ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4);
   rotate(radians(72));
   circles[i] = Circle;
  }

  myStem.drawStem();

}

class Stem { 
  int initalloX=200;
  int initalloY=800;

  Stem(int tempInitalloX, int tempInitalloY) {
    initalloX = tempInitalloX;
    initalloY = tempInitalloY;

  }

  void drawStem() {
    background(#0DBADB);
    scale(scaleFactor, scaleFactor);
    stroke (12, 149, 11);
    fill (12, 149, 11);
    strokeWeight(10);
    line(initalloX, initalloY, initalloX, ((frameCount>250)?initalloY-500:initalloY-(2*frameCount)));
    //stem1
    if (frameCount>101) {
      noStroke();
      translate(initalloX, initalloY-200);
      scale(min((float)(frameCount-100)/100, 1), min((float)(frameCount-100)/100, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(1/min((float)(frameCount-100)/100, 1), 1/min((float)(frameCount-100)/100, 1));
      translate(-initalloX, -(initalloY-200));
    }
    //stem2
    if (frameCount>151) {
      noStroke();
      translate(initalloX, initalloY-300);
      scale(-min((float)(frameCount-150)/150, 1), min((float)(frameCount-150)/150, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(-1/min((float)(frameCount-150)/150, 1), 1/min((float)(frameCount-150)/150, 1));
      translate(-initalloX, -(initalloY-300));
    }
  }
}

class Circle {

  int c1 = 0;
  int c2 = -40;
  int c3 = 50;
  int c4 = 50;

  Circle(int tc1, int tc2, int tc3, int tc4) {
    c1 = tc1;
    c2 = tc2;
    c3 = tc3;
    c4 = tc4;
  }
 }

在此先感谢...非常感谢所有帮助。

4

4 回答 4

1

除了已经指出的所有内容,请注意 ellipse() 是一个 void 方法,因此它不会返回任何内容。因此,像这样的行 circle = ellipse(x,y,z,z) 没有任何意义。您可能想使用存储在 ciclcle[i] 中的值来绘制椭圆,因此 ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4); 无需分配它。我也不明白为什么要创建 5 个相等的圆圈。如果你的圆形对象只是存储数据,为什么要存储五次相同的数据呢?来电:

for (int i = 0; i < circles.length; i++) {
ellipse(0, -40, 50, 50);
rotate(radians(72));
}

会有同样的效果。

除了在绘制结束时调用 background()(通过 myStem.drawStem())将隐藏之前绘制的所有内容。然而,无需每秒重新创建数组并重新分配值 60 次,您可以将其移至设置。

我对您的代码进行了这些更改。它现在将编译。仍然在原点绘制“花瓣”,并且需要处理它们的填充/笔划,但至少它正在运行:)您可能想在您的圆形类中创建一个显示方法......更像我指出的在你发表的另一篇文章中。干杯!

Stem myStem;

//Circle circles; // double declaration
  Circle circles[]; // keeping the array one only

float scaleFactor=0.5;

void setup() {
  size(floor(400*scaleFactor), floor(800*scaleFactor));
  myStem = new Stem(200,800);

  //mpoved this to setup, no need to recreate each frame
  circles = new Circle[5];
  circles[0]  = new Circle(0, -40, 50, 50);
  circles[1]  = new Circle(0, -40, 50, 50);
  circles[2]  = new Circle(0, -40, 50, 50);
  circles[3]  = new Circle(0, -40, 50, 50);
  circles[4]  = new Circle(0, -40, 50, 50);
  // also smooth only needs to be called once
  // unless ther is a noSmooth() somewhere
  smooth();

}

void draw() {

  // moved this here
  background(#0DBADB);

  for (int i = 0; i < circles.length; i++) {
   ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4);
   // note you may use this instead
   //ellipse(0, -40, 50, 50);
   rotate(radians(72));
  }

  myStem.drawStem();


}



class Stem { 
  int initalloX=200;
  int initalloY=800;

  Stem(int tempInitalloX, int tempInitalloY) {
    initalloX = tempInitalloX;
    initalloY = tempInitalloY;

  }

  void drawStem() {
    //background(#0DBADB); // this was hiding all other draws
    scale(scaleFactor, scaleFactor);
    stroke (12, 149, 11);
    fill (12, 149, 11);
    strokeWeight(10);
    line(initalloX, initalloY, initalloX, ((frameCount>250)?initalloY-500:initalloY-(2*frameCount)));
    //stem1
    if (frameCount>101) {
      noStroke();
      translate(initalloX, initalloY-200);
      scale(min((float)(frameCount-100)/100, 1), min((float)(frameCount-100)/100, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(1/min((float)(frameCount-100)/100, 1), 1/min((float)(frameCount-100)/100, 1));
      translate(-initalloX, -(initalloY-200));
    }
    //stem2
    if (frameCount>151) {
      noStroke();
      translate(initalloX, initalloY-300);
      scale(-min((float)(frameCount-150)/150, 1), min((float)(frameCount-150)/150, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(-1/min((float)(frameCount-150)/150, 1), 1/min((float)(frameCount-150)/150, 1));
      translate(-initalloX, -(initalloY-300));
    }
  }
}

class Circle {

  int c1 = 0;
  int c2 = -40;
  int c3 = 50;
  int c4 = 50;

  Circle(int tc1, int tc2, int tc3, int tc4) {
    c1 = tc1;
    c2 = tc2;
    c3 = tc3;
    c4 = tc4;
  }
 }
于 2012-11-20T14:27:50.060 回答
1

我想为声明一个数组学到了一些新东西。

至于出了什么问题,看起来您正在使用一个名为“circle”的 Circle 变量,并将其与 Circles 数组混淆,还称它为 circle,这可能会导致各种问题。这可能是您应该重点解决的问题。

于 2012-11-20T07:08:53.273 回答
1

猜...

类中有两种圆的定义

        Circle circles

        Circle[] circles
于 2012-11-20T07:23:42.020 回答
1

我认为这circles[i] = Circle;是错误。您不能将类型(类 Circle)分配给变量(即对象或类的实例)

于 2012-11-20T07:24:23.063 回答