Delay in Generating Moving Boxes

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.

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?

1 Like

You shall not call setup ().

Especially the size command does a lot behind the scenes

Instead increase the speed of the ball

This is my current program:

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??

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 :

 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 :

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

Full sketch:


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();
  }
}
//
1 Like