# Delaying function blocks?

**URL:** <https://discourse.processing.org/t/delaying-function-blocks/12054>\
**Category:** Coding Questions\
**Created:** [June 14, 2019, 3:20am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054 "2019-06-14T03:20:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [June 14, 2019, 3:20am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/1 "2019-06-14T03:20:35Z")

</div>

Hi there, I am very very new to processing.

I have created 4 different function blocks. Is there a way to have one function run for 3 seconds, and then switch to the next function for 3 seconds, etc, etc? when I put them all in the draw block, they all overlap so I need some sort of delay between functions.

Thanks so much!!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [June 14, 2019, 7:43am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/2 "2019-06-14T07:43:17Z")

</div>

```auto
int cur = int((millis()%12000)/3000);
if(cur == 0) A();
if(cur == 1) B();
if(cur == 2) C();
if(cur == 3) D();

```

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [June 14, 2019, 9:58pm UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/3 "2019-06-14T21:58:15Z")

</div>

Thank you so so much! If I wanted to add a “if key pressed…freeze this function and start new function”, would you happen to know how to do that? each function is just a drawing of shapes. I just want the shape to stay when key pressed, and then start new function where shape can stay when key pressed.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [June 15, 2019, 12:59am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/4 "2019-06-15T00:59:01Z")

</div>

> [@Pbchocochipcheerios](#):
>
> when key pressed

do you mean

> while key pressed

or

> after key pressed

> **[keyPressed() / Reference](https://processing.org/reference/keyPressed_.html)**
>
> The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable. For non-ASCII keys, use the keyCode varia…

> **[keyReleased() / Reference](https://processing.org/reference/keyReleased_.html)**
>
> The keyReleased() function is called once every time a key is released. The key that was released will be stored in the key variable. See key and keyCode for more informati…

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [June 15, 2019, 1:18am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/5 "2019-06-15T01:18:24Z")

</div>

oops, sorry I meant after I press a key and release it, I want it to keep the drawing on the screen until i stop it!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [June 15, 2019, 1:36am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/6 "2019-06-15T01:36:55Z")

</div>

that is without your timer…  
just a ?scene?level?

```auto
int screen = 0;

void setup() {
}
void draw() {
  switch (screen) {
  case 0:
    background(255);
// show shape a
    break;
  case 1:
    background(200,200,0);
// show shape b
    break;
  case 2:
    background(0,200,200);
// show shape c
    break;
  case 3:
    background(200,0,200);
// show shape d
    break;
  }
}
void keyPressed() {
  if ( key == '0' ) screen = 0;
  if ( key == '1' ) screen = 1;
  if ( key == '2' ) screen = 2;
  if ( key == '3' ) screen = 3;
}

```

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [June 15, 2019, 3:00am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/7 "2019-06-15T03:00:19Z")

</div>

Hmm, maybe I am describing my issue poorly…I don’t think the above code would work in my case.

I have 4 different functions I want to run. Each function will cycle through 3 different shapes continuously until a key is pressed and released. When the key is pressed and released, whatever shape is on the screen will stay on the screen until the very end and the next function block will run. The next function block will also cycle through 3 other different shapes. Again, when key is pressed and released, whatever shape is on the screen will stay (there would be 2 shapes stuck on the screen now), etc, etc. So basically need to freeze a function, skip the rest of the block and start a new function when the key is pressed and released. It can be any key.

I appreciate all your help though, I really do!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [June 15, 2019, 3:55am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/8 "2019-06-15T03:55:46Z")

</div>

possibly it would be much better if you show YOUR code here, so we can get a better idea.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 15, 2019, 4:00am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/9 "2019-06-15T04:00:44Z")

</div>

This seems like a slot-machine. 🎰  
But only 1 shape is shown and chosen at a time. 🤔

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [June 15, 2019, 4:20am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/10 "2019-06-15T04:20:27Z")

</div>

Yes!!! Like a slot machine!!!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 15, 2019, 5:25am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/11 "2019-06-15T05:25:15Z")

</div>

- You should make a SlotShape `class`.
- It should be responsible to display a PImage for some seconds (or [_frameCount_](https://processing.org/reference/frameCount.html)) and change to next 1.
- And stop changing the PImage objects when **keyPressed()** or **mousePressed()** is called back.

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [June 15, 2019, 11:43am UTC](https://discourse.processing.org/t/delaying-function-blocks/12054/12 "2019-06-15T11:43:47Z")

</div>

So I have the function displaying for some seconds and then switching to the next shape by using mills:

int m = (milliseconds()/1000)%10;

void shapesCategory1()  
{  
if (m \< 3)  
{  
drawCircle();  
}  
if (m \< 6)  
{  
drawSquare();  
}  
if (m \< 9)  
{  
drawRectangle();  
}

the only way I know how to stop this Is by saying:

boolean keySelect;  
keySelect = false; (this is under void setup)

void keyPressed()  
{  
keySelect = !keySelect;  
}

and then under each draw shape add:

if (keySelect)  
{  
drawCircle();  
noLoop();  
}

but then it just stops completely. Is there a function similar to noLoop() that will let me jump to a different function block when key is pressed so that I can select the next set of shapes in another function?

I would paste my code but it is SO messy and barely makes any sense to me right now lol
