# Delay in Generating Moving Boxes

**URL:** <https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379>\
**Category:** Coding Questions\
**Created:** [June 29, 2019, 1:22pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379 "2019-06-29T13:22:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 1:22pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/1 "2019-06-29T13:22:04Z")

</div>

I’m attempting to make a game where boxes fly in from the left of the screen and the player must move the cursor to dodge them. However, when I add the delay where I believe it should go, the game stops working.

Here is my code:

```auto
import java.util.*;
import processing.serial.*;
float x, y1;
float[] y = new float[30];
Random ran = new Random();

void setup() {
  frameRate(60);
  size(600, 600, P2D);
  strokeWeight(4);
  noCursor();
  x = -3;
  y1 = ran.nextFloat();
  int i;
  for (i = 0; i < 30; i = i + 1){
    y[i] = ran.nextFloat()*600;
    println(y[i]);
  }
}

void draw() {
  background(204);
  line(mouseX, mouseY, pmouseX, pmouseY);
  for (int i = 0; i < 30; i++){
    rect(x, y[i], 15, 15);
    delay(2000);
  }
  translate(x, y1);
  x += 10;
  
}

```

If I add the delay outside of the for loop, it just generates all 15 boxes and then makes them move 10 pixels every 2 seconds.

Any help would be appreciated!

---

<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:** [June 29, 2019, 1:31pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/2 "2019-06-29T13:31:14Z")

</div>

Actually, don’t use delay()

instead, add a smaller number to x or use a timer

See that i goes to 30?

Let it go up to a new variable upperBound

Increase this slowly by 1 eg. when if(frameCount % 10 == 0) upperBound ++;

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 2:02pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/3 "2019-06-29T14:02:25Z")

</div>

I’m not sure if this solves my issue. I want make a box, delay, make another, delay, etc… I believe your solution would just delay their group movement, correct?

---

<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:** [June 29, 2019, 2:10pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/4 "2019-06-29T14:10:08Z")

</div>

No, upperBound would delay how many are visible

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 3:00pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/5 "2019-06-29T15:00:11Z")

</div>

Ok, thank you. Is there anyway you could write the code in format so I could see how to apply it?

---

<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:** [June 29, 2019, 3:53pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/6 "2019-06-29T15:53:52Z")

</div>

Before setup ():

int upperBound =0;

in draw ()

for loop with upperBound

And after the for loop the if clause like above

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 3:56pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/7 "2019-06-29T15:56:01Z")

</div>

This is what I’ve changed my code to. I have one moving box and a score counter. I just can’t get any other boxes to generate like I want.  
So far I have:  
1 Moving Box  
A Score Counter  
A code that closes that game if my cursor hits the box

Here it is:

```auto
import java.util.*;
import processing.serial.*;
float x, y1;
float[] y = new float[30];
float[] upperBound = new float[30];
Random ran = new Random();
int pOrM, score = 0;

void setup() {
  frameRate(30);
  size(600, 600, P2D);
  strokeWeight(4);
  noCursor();
  x = -3;
  y1 = ran.nextFloat();
  int i;
  for (i = 0; i < 30; i = i + 1){
    y[i] = ran.nextFloat()*570;
    println(y[i]);
  }
  for (i = 0; i < 30; i++){
     upperBound[i] = 1; 
  }
}

void draw() {
  background(204);
  fill(0);
  text(score, 15, 30);
  textSize(26);
  line(mouseX, mouseY, pmouseX, pmouseY);
  for (int i = 0, j = 0; i < upperBound[j] && j < 29; i++, j++){
    rect(x, y[j], 30, 30);
    if (x < mouseX && x + 30 > mouseX && y[j] < mouseY && y[j] + 30 > mouseY){
     System.exit(0); 
    }
    frameRate++;
    if (x + 15 > 601){
      if (x < 597){
        score++;
      }
    }
  }
  
  translate(x, y1);
  x += 10;
  
  
}

```

I just can’t get the next boxes to load. It seems as though my for loop only ever runs once.

---

<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:** [June 29, 2019, 4:04pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/8 "2019-06-29T16:04:02Z")

</div>

upperBound doesn‘t need to be an array

you forgot the if line were upperBound gets increased

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 4:07pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/9 "2019-06-29T16:07:05Z")

</div>

```auto
import java.util.*;
import processing.serial.*;
float x, y1;
float[] y = new float[30];
//float[] upperBound = new float[30];
float upperBound = 0;
Random ran = new Random();
int pOrM, score = 0;

void setup() {
  frameRate(30);
  size(600, 600, P2D);
  strokeWeight(4);
  noCursor();
  x = -3;
  y1 = ran.nextFloat();
  int i;
  for (i = 0; i < 30; i = i + 1){
    y[i] = ran.nextFloat()*570;
    println(y[i]);
  }
  /*for (i = 0; i < 30; i++){
     upperBound[i] = 1; 
  }*/
}

void draw() {
  background(204);
  fill(0);
  text(score, 15, 30);
  textSize(26);
  line(mouseX, mouseY, pmouseX, pmouseY);
  for (int i = -1, j = 0; i < upperBound && j < 29; i++, j++){
    rect(x, y[j], 30, 30);
    if (x < mouseX && x + 30 > mouseX && y[j] < mouseY && y[j] + 30 > mouseY){
     System.exit(0); 
    }
    frameRate++;
    if (x + 15 > 601){
      if (x < 597){
        score++;
      }
    }
    if (frameCount %15 == 0){
       upperBound++; 
    }
  }
  
  translate(x, y1);
  x += 10;
  
  
}

```

This code makes one block for about a half second and then every other block pops into frame.

---

<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:** [June 29, 2019, 4:15pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/10 "2019-06-29T16:15:05Z")

</div>

> [@Bigp0ppa](#):
>
> for (int i = 0; i \< 30; i++){

That’s the initial for loop you had

Why not use this and replace 30 with upperBound here?

Also use i as index and get rid of j

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 4:50pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/11 "2019-06-29T16:50:06Z")

</div>

When I correct it for this issue, I get an array out of bounds exception [30].

---

<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:** [June 29, 2019, 4:58pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/12 "2019-06-29T16:58:19Z")

</div>

Yes

I forgot to make the if clause so that it must stop at 30

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 5:48pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/13 "2019-06-29T17:48:40Z")

</div>

I dont want a delay on when they are visible. I’m needing a delay on when they are created.

---

<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:** [June 29, 2019, 5:54pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/14 "2019-06-29T17:54:02Z")

</div>

That’s the same. The user doesn’t see them so for him they are not created

When they move over the screen border reset x to a random value smaller than 0

x=random(-344,-144);

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 5:56pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/15 "2019-06-29T17:56:20Z")

</div>

Okay, I see what you are saying there. However, the code you have provided creates a box for a few seconds and then gives me an array out of bounds issue.

---

<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:** [June 29, 2019, 6:11pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/16 "2019-06-29T18:11:29Z")

</div>

> [@Chrisir](#):
>
> Yes
> 
> I forgot to make the if clause so that it must stop at 30

Have you done this?

Chrisir

---

<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:** [June 29, 2019, 6:13pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/17 "2019-06-29T18:13:38Z")

</div>

> [@Bigp0ppa](#):
>
> frameCount %15

The 15 tells how fast the next block appears

Please post your current entire sketch, mate.

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 29, 2019, 6:23pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/18 "2019-06-29T18:23:14Z")

</div>

I am currently away from my desktop, but I will try once I am able and let you know the result. Thank you.

---

<div class="post-metadata">

**Author:** ![Bigp0ppa](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Bigp0ppa](https://discourse.processing.org/u/Bigp0ppa)\
**Post date:** [June 30, 2019, 9:19pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/19 "2019-06-30T21:19:46Z")

</div>

This is the code I have now.

```auto
import java.util.*;
import processing.serial.*;
float x, y1;
float[] y = new float[30];
//float[] upperBound = new float[30];
float upperBound = 1;
Random ran = new Random();
int pOrM, score = 0;

void setup() {
  frameRate(30);
  size(600, 600, P2D);
  strokeWeight(4);
  noCursor();
  x = -3;
  y1 = ran.nextFloat();
  int i;
  for (i = 0; i < 30; i = i + 1){
    y[i] = ran.nextFloat()*570;
    println(y[i]);
  }
  /*for (i = 0; i < 30; i++){
     upperBound[i] = 1; 
  }*/
}

void draw() {
  background(204);
  fill(0);
  text(score, 15, 30);
  textSize(26);
  line(mouseX, mouseY, pmouseX, pmouseY);
  for (int i = 0, j = 0; i < upperBound && j < 30; i++, j++){
    rect(x, y[i], 30, 30);
    if (x < mouseX && x + 30 > mouseX && y[i] < mouseY && y[i] + 30 > mouseY){
     System.exit(0); 
    }
    frameRate++;
    if (x + 15 > 601){
      if (x < 597){
        score++;
        x = -5;
      }
    }
    if (frameCount %60 == 0){
       upperBound++; 
    }
  }
  
  translate(x, y1);
  x += 10;
  
  
}

```

This works perfectly fine for the first box. However, as soon as upperBound bumps up 1 within my if statement, all of the boxes appear rather than 1 more. How should I approach this?

---

<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:** [June 30, 2019, 9:28pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/20 "2019-06-30T21:28:06Z")

</div>

> [@Bigp0ppa](#):
>
> if (frameCount %60 == 0){ upperBound++; }

This must be outside the for loop

[Next page](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379.md?page=2)
