# Change variable alternatingly

**URL:** https://discourse.processing.org/t/change-variable-alternatingly/32230
**Category:** Coding Questions
**Created:** [September 13, 2021, 2:17pm UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230 "2021-09-13T14:17:29Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 13, 2021, 2:17pm UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/1 "2021-09-13T14:17:29Z")

</div>

how to change i to 2 every couple of seconds/frames and then back

```auto
  direction[1]= loadImage("left.png");
  direction[2]= loadImage("right.png");

 i;

void setup(){
fullScreen();
}
void draw(){
image(direction[i], imagex, height-(height/4), 50, 80);
}

```

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [September 13, 2021, 4:59pm UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/2 "2021-09-13T16:59:47Z")

</div>

I think you already have a good starting point. The easier way is to use `frameCount`. You can use modulus to “cycle” the frame count, for example `frameCount % 60` would return 0 to 59, and then repeats from 0. According to that value, change `i` from 1 to 2… I guess that’s enough to make it happen 🙂

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 13, 2021, 5:15pm UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/3 "2021-09-13T17:15:36Z")

</div>

this does not work. did i do something wrong?

```auto
 PImage[] direction= new PImage[3];
 int i=1;

void setup(){
fullScreen();
  direction[1]= loadImage("left1.png");
  direction[2]= loadImage("right.png");
}
void draw(){
  if(frameCount % 60==29){
  i=2;}
  if(frameCount % 60==0){
  i=1;}
image(direction[i], imagex, height-(height/4), 50, 80);
}

```

---

<div class="post-metadata">

### Author: ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)
#### Post date: [September 14, 2021, 8:57am UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/4 "2021-09-14T08:57:59Z")

</div>

Hi @MoonAlien822  
That should totally work… when I test it here it works fine. One little tweak, perhaps you’re doing it for a reason, but why make the direction array of size 3?

```auto
PImage[] directionImages = new PImage[2];
int directionIndex = 0;

void setup() {
  size(600, 600);
  directionImages[0]= loadImage("left1.png");
  directionImages[1]= loadImage("right.png");
}
void draw() {
    
  if(frameCount%60 == 0) {
    directionIndex = 0;
  }
  if(frameCount%60 == 29) {
    directionIndex = 1;
  }

  image(directionImages[directionIndex], 0, 0, width, height);
}

```

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 14, 2021, 5:15pm UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/5 "2021-09-14T17:15:28Z")

</div>

> [@rapatski](#):
>
> ```auto
> PImage[] directionImages = new PImage[2];
> int directionIndex = 0;
> 
> void setup() {
> size(600, 600);
> directionImages[0]= loadImage("left1.png");
> directionImages[1]= loadImage("right.png");
> }
> void draw() {
>     
> if(frameCount%60 == 0) {
> directionIndex = 0;
> }
> if(frameCount%60 == 29) {
> directionIndex = 1;
> }
> 
> image(directionImages[directionIndex], 0, 0, width, height);
> }
> 
> ```

for my head its easier to work with numbers higher than 0 when counting. arrays start at 0, so while im only using 1 and 2, there are still three

---

<div class="post-metadata">

### Author: ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)
#### Post date: [September 14, 2021, 8:04pm UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/6 "2021-09-14T20:04:57Z")

</div>

Haha okay… you do you! But did you get it to work??

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 14, 2021, 8:05pm UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/7 "2021-09-14T20:05:58Z")

</div>

yes i did! It works in my full code too, thank you so much

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [October 2, 2021, 9:01am UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/8 "2021-10-02T09:01:11Z")

</div>

i would like to make the time way longer, how do i know what to set it to?

---

<div class="post-metadata">

### Author: ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)
#### Post date: [October 2, 2021, 9:30am UTC](https://discourse.processing.org/t/change-variable-alternatingly/32230/9 "2021-10-02T09:30:57Z")

</div>

It’s the “60” in `frameCount%60`. The frame count increases at the frame rate the sketch is set to, which by default is 60. The % operator gives you “the remains” of dividing the number by 60, which means that frameCount%60 will count from 0 to 59 every second. If you set it to a larger amount (120 would be two seconds), you’d increase the interval.
