# Delaying/Changing the speed of a section of code without delaying the entire code

**URL:** <https://discourse.processing.org/t/delaying-changing-the-speed-of-a-section-of-code-without-delaying-the-entire-code/38357>\
**Category:** Coding Questions\
**Created:** [August 15, 2022, 4:00am UTC](https://discourse.processing.org/t/delaying-changing-the-speed-of-a-section-of-code-without-delaying-the-entire-code/38357 "2022-08-15T04:00:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isobelcameron](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@Isobelcameron](https://discourse.processing.org/u/Isobelcameron)\
**Post date:** [August 15, 2022, 4:00am UTC](https://discourse.processing.org/t/delaying-changing-the-speed-of-a-section-of-code-without-delaying-the-entire-code/38357/1 "2022-08-15T04:00:55Z")

</div>

Hi!  
I am trying to delay expanding ball 2 so that it starts expanding after expanding ball 1. I have tried the pushMatrix(); and popMatrix(); which made my program crash and I also tried the delay example on the processing website: _[delay() / Reference / Processing.org](https://processing.org/reference/delay_.html)_. But couldn’t find out how to make it work. Would love some help if anyone knows how to fix this! :))

> \<

int tracker;  
float fade = 200;

float d = 25;  
float d2 = 25;  
float count = 0;

int step = 1;  
int max = 150;  
int max2 = 75;

void setup (){  
frameRate (30);  
size (1000,1000);  
background (255);  
noStroke();  
fill(0);  
}

void draw() {

background (255);  
fill (200);  
noStroke();

if (tracker\>20){  
ellipse(580,500,25,25);  
fill(fade,fade,fade);  
noStroke();  
fade=fade-0.9;  
}

fill(0);  
noStroke();  
circle(515,520,d); //expanding ball 1

count = count+1;  
d = d + step;  
if (d \> max){  
d = max;

}  
circle(600,780,d2); //expanding ball 2  
d2 = d2 + step;  
if (d2 \> max2){  
d2 = max2;

}  
}

> \<

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [August 15, 2022, 7:43am UTC](https://discourse.processing.org/t/delaying-changing-the-speed-of-a-section-of-code-without-delaying-the-entire-code/38357/2 "2022-08-15T07:43:56Z")

</div>

Hi @Isobelcameron,

would you first please format you code according to this section

[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

Cheers  
— mnse

---

<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:** [August 15, 2022, 7:55am UTC](https://discourse.processing.org/t/delaying-changing-the-speed-of-a-section-of-code-without-delaying-the-entire-code/38357/3 "2022-08-15T07:55:43Z")

</div>

> [@Isobelcameron](#):
>
> delay expanding ball 2 so that it starts expanding after expanding ball 1

It think it’s not about speed.

It’s more about animating one thing when the first thing is finished.

There are a lot of ways to do this.

One idea is that you set a marker (boolean) and evaluate it. Only when it’s true, the second ball growth

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [August 15, 2022, 12:36pm UTC](https://discourse.processing.org/t/delaying-changing-the-speed-of-a-section-of-code-without-delaying-the-entire-code/38357/4 "2022-08-15T12:36:38Z")

</div>

Hi @Isobelcameron,

Here’s an example of how you can do …  
(the code is lengthily written for demonstration, and could be written quite more compact)

Cheers  
— mnse

```auto
int d1 = 1;
int d1step = 2;
int d1max;

int d2 = 1;
int d2step = 2;
int d2max;

int direction = 1;

void setup() {
  size(500, 500);
  d1max = d2max = width/2;
}

void draw() {
  background(0);
  if (direction > 0) {
    if (d1 < d1max) {
      d1 += d1step;
    } else if (d2 < d2max) {
      d2 += d2step;
    } else {
      direction *= -1;
      d1step *= -1;
      d2step *= -1;
    }
  } else {
    if (d1 > 1) {
      d1 += d1step;
    } else if (d2 > 1) {
      d2 += d2step;
    } else {
      direction *= -1;
      d1step *= -1;
      d2step *= -1;
    }
  }
  circle(width*.25, height*.5, d1);
  circle(width*.75, height*.5, d2);
}

```

![video](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/d/1/d10edd43d04a79f32a1221d86f43fd3ddbcd0979.webp)

---

<div class="post-metadata">

**Author:** ![Isobelcameron](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@Isobelcameron](https://discourse.processing.org/u/Isobelcameron)\
**Post date:** [August 15, 2022, 1:26pm UTC](https://discourse.processing.org/t/delaying-changing-the-speed-of-a-section-of-code-without-delaying-the-entire-code/38357/5 "2022-08-15T13:26:25Z")

</div>

Thank you so much! that looks great will definitely implement that into my code 🙂
