# 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:** 4\
**Page:** 2

<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:** [July 1, 2019, 2:00am UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/21 "2019-07-01T02:00:58Z")

</div>

Oh, okay. Thank you very much! That solution seems to have worked.

I have one more question. Is there anyway to delete a previous box?

EDIT: Nvm, I figured it out.

EDIT 2:

New question. Sorry.

Why is it that with this code, my framerate isn’t bumping like it is supposed to.

```auto
import java.util.*;
import processing.serial.*;
float x, y1;
float y;
Random ran = new Random();
int pOrM, score = 0;
float fr = 30;
String st = "Press Any Key";
final int stateWaitBeforeProgram = 0;
final int stateNormalProgram = 1;
int state = stateWaitBeforeProgram;

void setup() {
  frameRate(fr);
  size(600, 600, P2D);
  strokeWeight(4);
  if (state == stateWaitBeforeProgram){
    x = -3;
  }
  y1 = ran.nextFloat();
  y = ran.nextFloat() * 570;
}

void draw() {
  if (state == stateWaitBeforeProgram){
    text(st, 260, 290);
  } else {
    noCursor();
    background(204);
    fill(0);
    text(score, 15, 30);
    textSize(26);
    line(mouseX, mouseY, pmouseX, pmouseY);
    rect(x, y, 30, 30);
    if (x < mouseX && x + 30 > mouseX && y < mouseY && y + 30 > mouseY){
      System.exit(0); 
    }
    if (x + 15 > 601){
      if (x < 597){
        score++;
        x = -100;
        fr += 5;
        setup();
      }
    }
    translate(x, y1);
    x += 10;
  }
  
}

void newFunc(){
    fr += 10;
    setup();
}

void keyPressed(){
   if (state == stateWaitBeforeProgram){
      state = stateNormalProgram; 
   }
}

```

Within the for loop where I bump fr and recall setup(), the first two lines of code initiate. However, neither the frame bump nor the call for setup() work. Why is 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:** [July 1, 2019, 5:48am UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/22 "2019-07-01T05:48:58Z")

</div>

You shall not call setup ().

Especially the size command does a lot behind the scenes

Instead increase the speed of the ball

---

<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:** [July 1, 2019, 2:36pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/24 "2019-07-01T14:36:25Z")

</div>

This is my current program:

```auto
import java.util.*;
float x, y1, x1;
float y;
Random ran = new Random();
int pOrM, score = 0, rectX, rectY;
float fr = 30;
String st = "Press Any Key";
final int stateWaitBeforeProgram = 0;
final int stateNormalProgram = 1;
int state = stateWaitBeforeProgram;

void setup() {
  frameRate(fr);
  size(600, 600, P2D);
  strokeWeight(4);
  if (state == stateWaitBeforeProgram){
    x = -3;
  }
  y1 = ran.nextFloat();
  y = ran.nextFloat() * 570;
}

void draw() {
  if (state == stateWaitBeforeProgram){
    text(st, 260, 290);
    x1 = 0;
    rectX = 30;
    rectY = 30;
  } else {
    noCursor();
    background(204);
    fill(0);
    text(score, 15, 30);
    textSize(26);
    line(mouseX, mouseY, pmouseX, pmouseY);
    rect(x, y, rectX, rectY);
    if (x < mouseX && x + 30 > mouseX && y < mouseY && y + 30 > mouseY){
      System.exit(0); 
    }
    if (x + 15 > 601){
      if (x < 601){
        score++;
        x = -100;
        y = ran.nextFloat() * 570;
        x1 += .5;
        rectX += 2;
        rectY += 2;
      }
    }
    translate(x, y1);
    x += 10 + x1;
  }
  
}

void keyPressed(){
   if (state == stateWaitBeforeProgram){
      state = stateNormalProgram; 
   }
}

```

My program, however, freezes after 16 points. Is there any way to fix 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:** [July 3, 2019, 6:46pm UTC](https://discourse.processing.org/t/delay-in-generating-moving-boxes/12379/25 "2019-07-03T18:46:46Z")

</div>

Sure.

This is causing the sketch to exit: System.exit(0);

**Your sketch stopped to show rects**

Also, even without this line, your sketch stopped to show rects.

That was because :

```auto
 if (x + 15 > 601) {
      if (x < 601) {

```

because when x1 was quite high, this line: `x += 10 + x1;` led to a jump when increasing x that x was beyond 601. So the condition was not met anymore.

So better :

```auto
 if (x + 15 > 601) {
      // if (x < 601) {

```

**Full sketch:**

```auto

import java.util.*;

float x, y1, x1;
float y;
Random ran = new Random();
int pOrM, score = 0, rectX, rectY;
float fr = 30;
String st = "Press Any Key";
final int stateWaitBeforeProgram = 0;
final int stateNormalProgram = 1;

int state = stateWaitBeforeProgram;

void setup() {
  frameRate(fr);
  size(600, 600, P2D);
  strokeWeight(4);
  if (state == stateWaitBeforeProgram) {
    x = -3;
  }
  y1 = ran.nextFloat();
  y = ran.nextFloat() * 570;
}

void draw() {
  if (state == stateWaitBeforeProgram) {
    background(204);
    text(st, 260, 290);
    x1 = 0;
    rectX = 30;
    rectY = 30;
  } else if (state == stateNormalProgram) {
    background(204);
    fill(0);
    text(score, 15, 30);
    textSize(26);
    line(mouseX, mouseY, pmouseX, pmouseY);
    rect(x, y, rectX, rectY);
    if (x < mouseX && x + 30 > mouseX && y < mouseY && y + 30 > mouseY) {
      // System.exit(0);
    }
    if (x + 15 > 601) {
      //if (x < 601) {
      println("here"+millis());
      score++;
      x = random(-120, -80);
      y = ran.nextFloat() * 570;
      y = random(570); 
      x1 += .5;
      rectX += 2;
      rectY += 2;
      // }
    }
    // translate(x, y1);
    x += 10 + x1;
  } else {
    // Error
    println("Error 317"); 
    exit();
  }//else 
  //
}

void keyPressed() {
  if (state == stateWaitBeforeProgram) {
    state = stateNormalProgram;
    noCursor();
  }
}
//

```

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