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

**URL:** <https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232>\
**Category:** Beginners\
**Created:** [July 13, 2021, 12:44pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232 "2021-07-13T12:44:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 12:44pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/1 "2021-07-13T12:44:51Z")

</div>

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!

```auto

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 12:49pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/2 "2021-07-13T12:49:50Z")

</div>

Easy. The class bracket } comes in too early.

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

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 12:51pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/3 "2021-07-13T12:51:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 12:52pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/4 "2021-07-13T12:52:31Z")

</div>

```auto

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()
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 12:53pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/5 "2021-07-13T12:53:43Z")

</div>

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

Then you see strange looking

```auto
}
}

```

right away

* * *

in your old code :

> [@LuckyDots](#):
>
> ```auto
> triangle(x, y, x+25, y+50, x-25, y+50);
> 
> }
> }
> }
> } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<--------------------------------
> 
> void change(){
> 
> ```

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 12:55pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/6 "2021-07-13T12:55:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 12:57pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/7 "2021-07-13T12:57:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 1:12pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/8 "2021-07-13T13:12:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 1:17pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/9 "2021-07-13T13:17:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 1:17pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/10 "2021-07-13T13:17:25Z")

</div>

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)_

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 1:21pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/11 "2021-07-13T13:21:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 1:29pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/12 "2021-07-13T13:29:34Z")

</div>

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

you are almost there

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 1:35pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/13 "2021-07-13T13:35:53Z")

</div>

```auto

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. 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 1:48pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/14 "2021-07-13T13:48:57Z")

</div>

> [@LuckyDots](#):
>
> tried moving the for loop to draw and got the error

Can’t see this above

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 1:51pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/15 "2021-07-13T13:51:10Z")

</div>

> [@LuckyDots](#):
>
> cannot find a class or type named _triangles_

It’s case sensitive

Chrisir

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 2:06pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/16 "2021-07-13T14:06:59Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 2:37pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/17 "2021-07-13T14:37:17Z")

</div>

```auto
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!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 2:47pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/18 "2021-07-13T14:47:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LuckyDots](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LuckyDots](https://discourse.processing.org/u/LuckyDots)\
**Post date:** [July 13, 2021, 9:44pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/19 "2021-07-13T21:44:40Z")

</div>

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](https://processing.org/examples/arrayobjects.html) 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.

```auto
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();
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 9:50pm UTC](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232/20 "2021-07-13T21:50:19Z")

</div>

The draw for loop is similar to the one in setup

Just for loop over the array as you had it previously

[Next page](https://discourse.processing.org/t/not-sure-why-my-array-is-not-allowed-to-use-a-function-oop/31232.md?page=2)
