Not sure why my array is not allowed to use a function (OOP)

Please use black here later

  • I think the time stuff properly should all go into the class so it can be different for all triangles

  • Don’t use the random throughout but only initially in the constructor and when the condition is met

just to demonstrate the idea of objects in an array here is an example (without timer) (OOP)

  • Please note that the class knows only ONE triangle. It is oblivious of the entire array. In fact you could just declare a single triangle before setup() without having to change the class. Good.

Triangle[] tri = new Triangle[18];

void setup() {
  size(900, 900);
  background(220);

  for (int i = 0; i < tri.length; i++) {
    float y = i*10;
    float x = 60*i;
    tri[i] = new Triangle(x, y);
  }
}//func

void draw() {
  background(220);

  for (int i = 0; i < tri.length; i++) {
    tri[i].change();
    tri[i].display();
  }
}//func

// ========================================================

class Triangle {

  // representing ONE Triangle

  int   savedTime = 11;
  float totalTime = random(0, 1100);

  float black = random(255);

  float x;
  float y;

  //constr 
  Triangle(float tempX, float tempY) {
    x = tempX;
    y = tempY;
  }//constr 

  void display() {
    fill(black);
    triangle(x, y, 
      x+25, y+50, 
      x-25, y+50);
  }//method

  void change() {
    black += 1;
    if (black >= 255) {
      black = 0;
    }
  }//method
  //
}//class
//

1 Like