Delay in Generating Moving Boxes

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:

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!

1 Like

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 ++;

1 Like

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?

No, upperBound would delay how many are visible

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

Before setup ():

int upperBound =0;

in draw ()

for loop with upperBound

And after the for loop the if clause like above

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:

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.

1 Like

upperBound doesn‘t need to be an array

you forgot the if line were upperBound gets increased

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.

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

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

Yes

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

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

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);

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.

Have you done this?

Chrisir

The 15 tells how fast the next block appears

Please post your current entire sketch, mate.

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

1 Like

This is the code I have now.

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?

1 Like

This must be outside the for loop