Hi! I need some help in spawning ellipses at a set interval!

Hi, I was able to create the functionality of almost everything in the game I’m making, except the spawning of the orbs at a set interval. I looked up some forums and attempted multiple methods, but I’m stuck. Essentially my objective is to make the function orbSpwn(); multiply at a set interval in order to make the game infite. I am just getting to know processing so any help is very useful. Here’s the code for the game:

boolean[] keys = new boolean[3];
ship s;
gun g;
ArrayList <orb> o;
ArrayList<bullet> b;
int q = 5;

void setup() {
  size(700, 700);
  background(#020019);
  rectMode(CENTER);

  s = new ship();

  o = new ArrayList<orb>();
  for (int i=0; i<q; i++) {
    o.add( new orb());
  }

  b = new ArrayList<bullet>();

  g = new gun();
}






void draw() {
  g.rotatGun();
  g.gunDisplay();

  // Need to loop over list, updating, drawing, and deleting

  { 
    for (int f=0; f<b.size(); f++) {
      if ( b.get(f).vel()) {
        // remove bullet from ArrayList
        b.remove(f);
      } else {
        //Drawing bullet
        b.get(f).render();
      }
    }
  }

  s.shipD();
  orbSpwn();
}






////////////////////////////////////////////////////////////////////////////////////////
void mousePressed() {
  PVector muzzle = g.muzzlePosition();
  b.add ( new bullet ( muzzle.x, muzzle.y, -3 * sin(g.r), -3 * cos(g.r) ) );
}







////////////////////////////////////////////////////////////////////////////////////////
void orbSpwn() {
  //Orb Loop Creation
  for (int i=0; i<o.size(); i++) {
    o.get(i).orbD();
    o.get(i).drop();

    //Deleting the orb when colliding
    for (int c = 0; c<b.size(); c++) {
      float d = dist(b.get(c).xPosition, b.get(c).yPosition, o.get(i).xO, o.get(i).yO);
      if (d < b.get(c).radB + o.get(i).radO) {
        o.get(i).radO = 0;
        o.get(i).radO2 = 0;
      }
    }
  }
}




////////////////////////////////////////////////////////////////////////////////////////
//Allows for the rotation to remember its position
void keyPressed() {
  buttons(true);
}
void keyReleased() {
  buttons(false);
}
void buttons(boolean n) { 
  if (key == 'd') keys[0] = n;
  if (key == 'a') keys[1] = n;
}  
class bullet {
  float xPosition = 350, yPosition = 525;
  float xVelocity, yVelocity;
  float radB = 1.5;

  bullet( float x, float y, float vX, float vY ) {
    xPosition = x;
    yPosition = y;
    xVelocity = vX;
    yVelocity = vY;
  }
  
  

  Boolean vel() {
    xPosition -= xVelocity;
    yPosition += yVelocity;
    if ( xPosition > width || yPosition >height) {
      return true;
    } else {
      return false;
    }
  }
  
  

  void setDir(float dir, float xSpd, float ySpd) {
    dir = g.r;
    xVelocity = xSpd * sin(dir);
    yVelocity = ySpd * cos(dir);
  }

  void render() {
    rectMode(CENTER);
    fill(255);
    ellipse(xPosition, yPosition, radB*2, radB*2);
  }
}

class gun {
  float x;
  float y;
  float xv = 0;
  float yv= -68.774;
  float r = 0; 

  gun() {
    //The x and y are placed along here to make coding easier and more understanble
    x = 350;
    y = 600;
  }

  //Rotates the gun according to which key is pressed
  void rotatGun() {
    //Creates the if and else needed for rotation
    r+=(keys[0]?.05:0);
    r%=TWO_PI;

    r+=(keys[1]? -.05:0);
    r%=TWO_PI;
  }

  PVector muzzlePosition() {
    return new PVector ( x + 68.774 * sin(r), y - 68.774 * cos(r) );
  }

  void gunDisplay() {
    background(#020019);

    //Gun Base
    fill(#3F3F3F);
    pushMatrix();
    translate(x, y);
    rotate(r);
    //Changes the base rect in order to have a fixed point for bullet
    beginShape();
    vertex(-5.3, -68.774);
    vertex(xv, yv);
    vertex(5.3, -68.774);
    vertex(5.3, 9.422);
    vertex(-5.3, 9.422);
    vertex(-5.3, -68.774);
    endShape();
    popMatrix();

    //Gun Top
    fill(#BFBFBF);
    pushMatrix();
    translate(x, y);
    rotate(r);
    rect(0, -31, 5.847, 70);
    popMatrix();
  }
}

class orb {
  float xO = random(10, 690);
  float yO = random(-50, -20);
  float radO = random(8.7, 26.1);
  float radO2 = 3.535;
  float oSpd = random(0.3, 1.3);






  void drop() {
    yO+=oSpd;
  }



  void orbD() {

    //Orb Base
    fill(#870A0A);
    ellipse(xO, yO, radO*2, radO*2);

    //Orb Center
    fill(#000000);
    ellipse(xO, yO, radO2*2, radO2*2);
  }
}

class ship {

  float x = 350;
  float y = 600;

  void shipD() {

    //Ship Base
    pushMatrix();
    translate(x, y);
    stroke(#FFFFFF);
    stroke(2);
    fill(#3F3F3F);
    rect(0.439, 20.422, 60.879, 62.012);

    //Ellipse 1
    noStroke();
    fill(#6D6D6D);
    ellipse(0.439, 20.422, 18.544, 18.544);

    //Ellipse 2
    fill(#BFBFBF);
    ellipse(0.439, 20.422, 6.475, 6.475);

    //Line Design 1
    stroke(#B7B7B7);
    stroke(1);
    line(-28.486, -7.884, -10.233, 10.369);

    //Line Design 2
    line(-10.211, 10.369, -10.211, 30.529);

    //Line Design 3
    line(-10.233, 30.537, -28.486, 48.789);
    popMatrix();

    //Line Design 4
    pushMatrix();
    translate(x, y);
    line(-27.941, -8.429, -10.312, 9.824);

    //Line Design 5
    line(-10.007, 9.683, 10.153, 9.683);

    //Line Design 6
    line(28.406, -8.429, 10.153, 9.824);

    //Line Design 7
    line(-10.359, 31.073, 10.519, 31.073);

    //Line Design 8
    line(10.97, 10.342, 10.97, 30.502);

    //Line Design 9
    line(29.223, -7.475, 10.97, 10.505);

    //Line Design 10
    line(-27.851, 49.326, -10.402, 31.073);

    //Line Design 11
    line(29.359, 48.871, 11.107, 30.618);

    //Line Design 12
    line(28.725, 49.326, 10.472, 31.073);

    //Trapezoid Design 1
    noFill();
    stroke(#B7B7B7);
    beginShape();
    vertex(-27.987, -3.883);
    vertex(-12.775, 10.369);
    vertex(-12.775, 30.392);
    vertex(-27.987, 45.104);
    vertex(-27.987, -3.883);
    endShape();
    popMatrix();

    //Trapezoid Design 2
    pushMatrix();
    translate(x, y);
    noFill();
    beginShape();
    vertex(-23.931, 48.623);
    vertex(-10.32, 33.411);
    vertex(10.344, 33.411);
    vertex(25.055, 48.623);
    vertex(-23.931, 48.623);
    endShape();

    //Trapezoid Design 3
    beginShape();
    vertex(28.793, -4.071);
    vertex(28.793, 44.915);
    vertex(13.581, 30.664);
    vertex(13.581, 10.64);
    vertex(28.793, -4.071);
    endShape();

    //Trapezoid Design 4
    beginShape();
    vertex(24.566, -7.884);
    vertex(10.315, 7.328);
    vertex(-10.291, 7.328);
    vertex(-24.42, -7.884);
    vertex(24.566, -7.884);
    endShape();

    //Trapezoid Design 5
    fill(#BFBFBF);
    beginShape();
    vertex(-23.244, 11.394);
    vertex(-17.519, 16.756);
    vertex(-17.519, 24.292);
    vertex(-23.244, 29.828);
    vertex(-23.244, 11.394);
    endShape();

    //Trapezoid Design 6
    beginShape();
    vertex(-9.144, -3.14);
    vertex(9.29, -3.14);
    vertex(3.927, 2.584);
    vertex(-3.608, 2.584);
    vertex(-9.144, -3.14);
    endShape();

    //Trapezoid Design 7
    beginShape();
    vertex(23.783, 11.394);
    vertex(23.783, 29.828);
    vertex(18.059, 24.465);
    vertex(18.059, 16.93);
    vertex(23.783, 11.394);
    endShape();

    //Trapezoid Design 8
    beginShape();
    vertex(9.29, 43.335);
    vertex(-9.144, 43.335);
    vertex(-3.781, 37.61);
    vertex(3.754, 37.61);
    vertex(9.29, 43.335);
    endShape();
    popMatrix();

    //OuterBox Line 1
    pushMatrix();
    stroke(#FFFFFF);
    translate(x, y);
    line(-30.001, -10.584, -30.001, 51.428);

    //OuterBox Line 2
    line(-30.001, 51.428, 30.879, 51.428);

    //OuterBox Line 3
    line(30.879, 51.428, 30.879, -10.584);

    //OuterBox Line 4
    line(30.879, -10.584, -30.001, -10.584);
    popMatrix();
  }
}

1 Like

Do you want the orbs to spawn, for example, every 5 seconds or do you want to do something else?

I want them to spawn every 5 seconds or so.

You would have to make some function which determines how long it has been since an orb has been last spawned. To find how long it has been, you use millis() and then subtract it from the millis value from the last time it spawned. You’d also need a function which gives you the amount of seconds instead of milliseconds.

int Seconds(){
   return  int(millis()/1000) - previousTime;
}

previousTime is the millis() value from the last time you spawned an orb

1 Like

In your draw loop, you’d check if (Seconds() == 5) and then you’d spawn an orb

Thank you for your help and I hate to be spoon fed with this, but when I placed all the information where it should go it doesn’t spawn anything. It seems as though the seconds do not get to five. I printed it out, and it jumps from 4 to -5.009. Is there something I’ve done wrong? (I cut out the rest of draw to make it easier to see).

void draw(){
if (Seconds() == 5) {
      orbSpwn();
  }
}
int Seconds() {
  return int(millis()/1000) - previousTime;
}
void orbSpwn() {
  previousTime = millis();
  //Orb Loop Creation
  for (int i=0; i<o.size(); i++) {
    o.get(i).orbD();
    o.get(i).drop();

    //Deleting the orb when colliding
    for (int c = 0; c<b.size(); c++) {
      float d = dist(b.get(c).xPosition, b.get(c).yPosition, o.get(i).xO, o.get(i).yO);
      if (d < b.get(c).radB + o.get(i).radO) {
        o.get(i).radO = 0;
        o.get(i).radO2 = 0;
      }
    }
  }
}
1 Like

You must have this in setup too

When you reassign previousTime’s you’d have to change it to seconds with
int(millis/1000)

Looks suspicious

Maybe move ) to the end?

What happens is in the draw loop, it checks if Seconds() == 5 but when previousTime is set, it is set in milliseconds, not seconds. This is subtracting the number of seconds by the number of milliseconds.

1 Like

Thank you both for your help so far, but I still seem to be missing a lot of it. PreviousTime now stops right before 5 and jumps back to 0. This is what I’ve done so far:

void setup(){
previousTime = int(millis()/1000);
}
if (Seconds() == 5) {
    orbSpwn();
  }
}
int Seconds() {
  return int((millis()/1000) - previousTime);
}
void orbSpwn() {
  previousTime = int(millis()/1000);
  //Orb Loop Creation
  for (int i=0; i<o.size(); i++) {
    o.get(i).orbD();
    o.get(i).drop();

    //Deleting the orb when colliding
    for (int c = 0; c<b.size(); c++) {
      float d = dist(b.get(c).xPosition, b.get(c).yPosition, o.get(i).xO, o.get(i).yO);
      if (d < b.get(c).radB + o.get(i).radO) {
        o.get(i).radO = 0;
        o.get(i).radO2 = 0;
      }
    }
  }
}
1 Like

return int(millis()/1000- previousTime);

previousTime = millis();

I just tried a program which uses the same concept and it worked, the only difference was that

int Seconds() {
  return int((millis()/1000) - previousTime);
}

did not have brackets, and it looked like

int Seconds() {
  return int(millis()/1000) - previousTime;
}

The only way that doing that can work, would be if was this

int Seconds() {
  return int((millis() - previousTime)/1000);
}

You don’t need to assign previousTime’s value at the start

You can just keep it as

previousTime = 0;

I fiddled around with it for a while and I can’t seem to make it work. Thank you both for your help, I’m looking for another way to make it work.

A quick test, this works exactly how you want it to.

https://www.openprocessing.org/sketch/722057

1 Like