# Change background in order/ using values in array

**URL:** https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665
**Category:** Coding Questions
**Created:** [July 16, 2020, 2:39pm UTC](https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665 "2020-07-16T14:39:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anderbaer](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@anderbaer](https://discourse.processing.org/u/anderbaer)
#### Post date: [July 16, 2020, 2:39pm UTC](https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665/1 "2020-07-16T14:39:28Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![anderbaer](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@anderbaer](https://discourse.processing.org/u/anderbaer)
#### Post date: [July 16, 2020, 2:46pm UTC](https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665/2 "2020-07-16T14:46:41Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)
#### Post date: [July 16, 2020, 2:47pm UTC](https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665/3 "2020-07-16T14:47:47Z")

</div>

> [@Sequence of actions from array](https://discourse.processing.org/t/sequence-of-actions-from-array/22480):
>
> Howdy! I want to create a sequence of events as determined by an array, but for some reason, only the last element of the array is accounted for. Also, I would like the actions to occur with a slight delay with respect to each other. The delay() function would not work as it would stop all the program, while I would need the program to keep on running during the delay. Below is some code to make a particular color square according to the integer stored in an array. The only square that seems …

---

<div class="post-metadata">

### Author: ![anderbaer](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@anderbaer](https://discourse.processing.org/u/anderbaer)
#### Post date: [July 16, 2020, 3:18pm UTC](https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665/4 "2020-07-16T15:18:31Z")

</div>

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 ☹

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [July 16, 2020, 4:29pm UTC](https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665/5 "2020-07-16T16:29:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [July 16, 2020, 8:29pm UTC](https://discourse.processing.org/t/change-background-in-order-using-values-in-array/22665/6 "2020-07-16T20:29:35Z")

</div>

Hi @anderbaer,

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

So when you are doing this:

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

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

```
