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

hello again, my problem is as follows.

I am have tried and somewhat suceeded in creating a function which creates a fade from black to white after a random amount of time and then resets itself.

i am trying to fill the screen with a grid of these triangles so they fade in at random times, i managed to do it by creating a new function for each triangle, after realising that this would result in thousands of lines of code i have used an array and a for loop to create a grid of triangles. Now i am stuck. for some reason which i do not understand it simply selects all of the triangles and fades them in at the same time. Not what i want to happen!


Triangles[] tri = new Triangles[18];

int savedTime;
float totalTime = 5000;
float black = 0;


void setup()
{
size(900, 900);
background(0);
savedTime = millis();
for(int i = 0; i < tri.length; i++){
float y = 0;
float x = 0;
tri[i] = new Triangles(x, y);
}
}



void draw()
{
change();

  for(int i = 0; i < tri.length; i++){

tri[i].display();

}

}




class Triangles {

float x;
float y;



Triangles(float tempX, float tempY){
 
  x = tempX;
  y = tempY;
  

}

void display()
{
     for(float x = 25; x < width; x+=50){
        for(float y = 0; y < height; y+=50){
triangle(x, y, x+25, y+50, x-25, y+50);

}
}
}
}

void change(){
int passedTime = millis() - savedTime;
totalTime = random(0, 15000);
if(passedTime > totalTime){
  noStroke();
  fill(black);
black += 1;
if(black == 255){
  black = 0;
savedTime = millis();
}
}
}

can anyone help me as i feel hopelessly stuck and the answer is just not presenting itself really.

when i try the following in the draw loop i get an error saying “The function change() does not exist.”
again i do not understand why this is.

void draw()
{

  for(int i = 0; i < tri.length; i++){

tri[i].change();
tri[i].display();

}

}

if anyone can explain where i am fundamentally going wrong or why this error is happening it would be greatly appreciated!

Thanks for your time.

Easy. The class bracket } comes in too early.

It is before the method change but must be after the entire method change()

can you highlight which bracket you are refferring to possibly? thankyou though :smiley:


Triangles[] tri = new Triangles[18];




void setup() {
  size(900, 900);
  background(0);
  for (int i = 0; i < tri.length; i++) {
    float y = 0;
    float x = 0;
    tri[i] = new Triangles(x, y);
  }
}

void draw() {
  background(0);

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


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

class Triangles {

  int   savedTime = millis();

  float totalTime = 5000;
  float black = 0;

  float x;
  float y;

  Triangles(float tempX, float tempY) {

    x = tempX;
    y = tempY;
  }//constr 

  void display() {
    for (float x = 25; x < width; x+=50) {
      for (float y = 0; y < height; y+=50) {
        triangle(x, y, x+25, y+50, x-25, y+50);
      }
    }
  }


  void change() {
    int passedTime = millis() - savedTime;
    totalTime = random(0, 15000);
    if (passedTime > totalTime) {
      noStroke();
      fill(black);
      black += 1;
      if (black == 255) {
        black = 0;
        savedTime = millis();
      }
    }
  }
}//class  ------------>>>>>>>>>>>  was before change()
//

It’s easier to hit ctrl-t to get auto indents.

Then you see strange looking

}
}

right away


in your old code :

ahh brilliant! thankyou again for the tip about the shortcut.

1 Like

okay, its probably best i try to figure this out on my own but can you give any pointers as to how i can get each triangle to fade in at a random time using the change function? They are all fading in at the same time.

Wow

Now, I moved the variables like int savedTime = millis();
already in the class because time shall be different for each triangle.

What to do

Now the class represents one triangle. ONE.

So display() should be brief: triangle(x, y, x+25, y+50, x-25, y+50);
(the for loop already is in draw() so NOT needed here )

Also you forgot to pass different values for x,y in setup() (because the false for-loop in display() took care of this). So in setup() calculate some values based on i.

Then use background(220); at start of draw() (NOT background(0); because black on black won’t work, you know… )

Now, go and fix your timer. 15 seconds is quite long though.

Got it working here.

can you post the code, i really cannot understand what to do?

no, of course not.

it’s really not hard

please update the page now since I changed my post

(it’s forum policy not to provide full code solutions here as you guessed)

oh i see, thankyou for the pointers, i will keep trying.

post your attempt and show your entire code and we will have a look

you are almost there



Triangles[] tri = new Triangles[18];

int savedTime;
float totalTime = 5000;
float black = 0;


void setup()
{
  size(900, 900);
  background(0);
  savedTime = millis();
  for (int i = 0; i < tri.length; i++) {
    float y = 0;
    float x = 0;
    tri[i] = new Triangles(x, y);
  }
}



void draw()
{
  background(220);
  for (int i = 0; i < tri.length; i++) {

    tri[i].change();
    tri[i].display();
  }
}




class Triangles {

  float x;
  float y;



  Triangles(float tempX, float tempY) {

    x = tempX;
    y = tempY;
  }

  void display()
  {
    for (float x = 25; x < width; x+=50) {
      for (float y = 0; y < height; y+=50) {
        triangle(x, y, x+25, y+50, x-25, y+50);
      }
    }
  }

  void change() {
    int passedTime = millis() - savedTime;
    totalTime = random(0, 5000);
    if (passedTime > totalTime) {
      noStroke();
      fill(black);
      black += 1;
      if (black == 255) {
        black = 0;
        savedTime = millis();
      }
    }
  }
}

I tried moving the for loop to draw and got the error
“cannot find a class or type named Triangles

Thankyou again. pointers are good though, I also don’t really want to just be handed everything on a plate here. :slight_smile:

1 Like

Can’t see this above

The for loop was already in draw()…!!!

It’s case sensitive

Chrisir

int savedTime;
float totalTime = 5000;
float black = 0;
Triangles[] tri = new Triangles[18];


void setup()
{
  size(900, 900);
  background(0);
  savedTime = millis();
  float y = 0;
  float x = 0;
  for (int i = 0; i < tri.length; i++) {

    tri[i] = new Triangles(x, y);
  }
}



void draw()
{
  background(220);


  for (float x = 25; x < width; x+=50) {
    for (float y = 0; y < height; y+=50) {
      for (int i = 0; i < tri.length; i++) {


        tri[i].change();
        tri[i].display();
      }
    }
  }




  class Triangles {

    float x = 0.0;
    float y =0.0;



    Triangles(float tempX, float tempY) {

      x = tempX;
      y = tempY;
    }



    void display()
    {

      triangle(x, y, x+25, y+50, x-25, y+50);
    }
  }



  void change() {
    int passedTime = millis() - savedTime;
    totalTime = random(0, 5000);
    if (passedTime > totalTime) {
      noStroke();
      fill(black);
      black += 1;
      if (black == 255) {
        black = 0;
        savedTime = millis();
      }
    }
  }
}

probably time to go back to the drawing board, i feel like this is just further away than before.

I really do appreciate the help though so thankyou.

Triangles tri1;

int savedTime;
float totalTime = 5000;
float black = 0;

void setup()
{
  size(900, 900);
  background(0);
  savedTime = millis();
  float x =25;
  float y =0;
  tri1 = new Triangles(x, y);
}

void draw()
{
  tri1.display();
}

class Triangles
{
  float x;
  float y;

  Triangles(float xpos, float ypos)
  {
    x = xpos;
    y = ypos;
  }


  void display()
  {
    color black = 0;
    fill(black);
     noStroke();
    float x;
    float y;
    for (x = 25; x < width; x+=50) {
      for (y = 0; y < height; y+=50) {
        int passedTime = millis() - savedTime;
        totalTime = random(0, 5000);
        if (passedTime > totalTime) {
         
          fill(black);
          triangle(x, y, x+25, y+50, x-25, y+50);
          black += 1;
          if (black == 255) {
            black = 0;
            savedTime = millis();
          }
        }
      }
    }
  }
}

okay i made some progress, thankyou for bearing with me, i’m aware its probably fustraiting when the solution is actually quite simple!

Well, actually no.

Not right approach.

You are doing the opposite of what I wrote.

class holds ONLY ONE triangle

For loop in setup sets different x for each triangle in the array (that you defined before draw())

In the class no for loops at all

For loop in draw

I mean your way cannot make different shades of black

Okay i’ve moved the for loop into draw, now just one triangle appears. Obviously not working as intended. Should the for loop for the X / Y be in setup? Should i be using variables within the Triangles class for the timer too? These are the only things that come to mind but i don’t want to be flogging a dead horse.

I tried modifying this example ArrayObjects \ Examples \ Processing.org but to no avail.

If you could point me towards any other resources i would be very appreciative, again thankyou for your time and apologies on my slowness.

int savedTime;
float totalTime = 5000;
float black = 0;

Triangles[] tri = new Triangles[18];

void setup() {
  size(900, 900);
  noStroke();
  savedTime = millis();
  background(0);
  tri = new Triangles[18];

  //  int index = 0;
  for (int i = 0; i < 18; i++) {
    float x =25;
    float y =0;
    tri[i] = new Triangles(x, y);
    println(i);
  }
}

void draw() {

  for (int y = 0; y < height; y+=50) {

    for (int x = 25; x < width; x+=50) {
      for (int i = 0; i < 18; i++) {
        tri[i].display();
      }
    }
  }
}

class Triangles
{
  float x;
  float y;

  Triangles(float xpos, float ypos)
  {
    x = xpos;
    y = ypos;
  }


  void display() {

    color black = 0;
    noStroke();


    int passedTime = millis() - savedTime;
    totalTime = random(0, 2500);
    if (passedTime > totalTime) {

      //   fill(black);
      fill(255);
      triangle(x, y, x+25, y+50, x-25, y+50);
      black += 1;
      if (black == 255) {
        black = 0;
        savedTime = millis();
      }
    }
  }
}

The draw for loop is similar to the one in setup

Just for loop over the array as you had it previously