Change background in order/ using values in array

int i = 0;
int[] array =  {250, 150, 100, 50, 0};

void setup() {
  size(500, 500);
}

void draw() {


  for (int i = 0; i < array.length; i++) {
    if (array[i]  == 0) {

      println(array);

      background(array[i]);
      stroke(255);
      strokeWeight(5);
      translate(width/2, height/2);
      rotate(radians(frameCount));
      noFill();
      ellipse(100, 100, 100, 100);
    }
  }
}
1 Like

Hey Guys, how do I change the colour of the background in order? Starting from 250, ending at 0 and then looping it? Thanks for your support :slightly_smiling_face:

1 Like

Thanks slow_izzm!! Unfortunately I still don’t get it right… I updated my code and now the values from the array run in order but I still can’t see the background changing :frowning:

Your colour values are all fully transparent so try
background(color(array[i]));

Hi @anderbaer,

Your screen is refreshed only after the end of the draw() function.

So when you are doing this:

int i = 0;
int[] array =  {250, 150, 100, 50, 0};

void setup() {
  size(500, 500);
}

void draw() {
  for (int i = 0; i < array.length; i++) {
     background(array[i]);
  }
}

You are indeed looping through all your background colors but only the last one is displayed. You can verify that by changing the last value of your array.

To loop through your background colors and see it change you need another variable that will hold the current background number and display this one.

For example, you can change the color of your background every 60 frames with the following code:

int[] array =  {250, 150, 100, 50, 150};
int currentBackground;

void setup() {
  size(500, 500);
  currentBackground = 0;
}

void draw() {
  if (frameCount % 60 == 0) {
    changeBackground();
  }
  
  background(array[currentBackground]);
}

void changeBackground() {
  currentBackground++;
  
  if (currentBackground == array.length) {
    currentBackground = 0;
  }
}
2 Likes