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

**URL:** <https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691>\
**Category:** Coding Questions\
**Created:** [May 29, 2019, 8:12pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691 "2019-05-29T20:12:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![imBrinCKER](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imbrincker/32/5407_2.png) [@imBrinCKER](https://discourse.processing.org/u/imBrinCKER)\
**Post date:** [May 29, 2019, 8:12pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/1 "2019-05-29T20:12:24Z")

</div>

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:

```auto
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;
}  

```

```auto
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);
  }
}

```

```auto
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();
  }
}

```

```auto
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);
  }
}

```

```auto
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();
  }
}

```

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 9:09pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/2 "2019-05-29T21:09:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imBrinCKER](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imbrincker/32/5407_2.png) [@imBrinCKER](https://discourse.processing.org/u/imBrinCKER)\
**Post date:** [May 29, 2019, 9:21pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/3 "2019-05-29T21:21:50Z")

</div>

I want them to spawn every 5 seconds or so.

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 9:28pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/4 "2019-05-29T21:28:55Z")

</div>

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.

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

```

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

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 9:36pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/5 "2019-05-29T21:36:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imBrinCKER](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imbrincker/32/5407_2.png) [@imBrinCKER](https://discourse.processing.org/u/imBrinCKER)\
**Post date:** [May 29, 2019, 9:53pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/6 "2019-05-29T21:53:45Z")

</div>

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

```auto
void draw(){
if (Seconds() == 5) {
      orbSpwn();
  }
}

```

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

```

```auto
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;
      }
    }
  }
}

```

---

<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:** [May 29, 2019, 9:57pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/7 "2019-05-29T21:57:22Z")

</div>

> [@imBrinCKER](#):
>
> previousTime = millis();

You must have this in setup too

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 9:58pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/8 "2019-05-29T21:58:37Z")

</div>

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

---

<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:** [May 29, 2019, 9:58pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/9 "2019-05-29T21:58:59Z")

</div>

> [@imBrinCKER](#):
>
> return int(millis()/1000) - previousTime;

Looks suspicious

Maybe move **)** to the end?

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 10:00pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/10 "2019-05-29T22:00:48Z")

</div>

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.

> [@Chrisir](#):
>
> > [@imBrinCKER](#):
> >
> > return int(millis()/1000) - previousTime;
> 
> Looks suspicious
> 
> Maybe move **)** to the end?

---

<div class="post-metadata">

**Author:** ![imBrinCKER](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imbrincker/32/5407_2.png) [@imBrinCKER](https://discourse.processing.org/u/imBrinCKER)\
**Post date:** [May 29, 2019, 10:10pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/11 "2019-05-29T22:10:17Z")

</div>

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:

```auto
void setup(){
previousTime = int(millis()/1000);
}

```

```auto
if (Seconds() == 5) {
    orbSpwn();
  }
}

```

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

```

```auto
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;
      }
    }
  }
}

```

---

<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:** [May 29, 2019, 10:13pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/12 "2019-05-29T22:13:17Z")

</div>

> [@imBrinCKER](#):
>
> return int((millis()/1000) - previousTime);

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

---

<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:** [May 29, 2019, 10:14pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/13 "2019-05-29T22:14:01Z")

</div>

> [@imBrinCKER](#):
>
> previousTime = int(millis()/1000);

previousTime = millis();

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 10:15pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/14 "2019-05-29T22:15:53Z")

</div>

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

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

```

did not have brackets, and it looked like

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

```

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 10:17pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/15 "2019-05-29T22:17:40Z")

</div>

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

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

```

> [@Chrisir](#):
>
> > [@imBrinCKER](#):
> >
> > previousTime = int(millis()/1000);
> 
> previousTime = millis();

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 10:25pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/16 "2019-05-29T22:25:26Z")

</div>

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

> [@imBrinCKER](#):
>
> void setup(){  
> previousTime = int(millis()/1000);  
> }

You can just keep it as

```auto
previousTime = 0;

```

---

<div class="post-metadata">

**Author:** ![imBrinCKER](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imbrincker/32/5407_2.png) [@imBrinCKER](https://discourse.processing.org/u/imBrinCKER)\
**Post date:** [May 29, 2019, 11:06pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/17 "2019-05-29T23:06:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 11:12pm UTC](https://discourse.processing.org/t/hi-i-need-some-help-in-spawning-ellipses-at-a-set-interval/11691/18 "2019-05-29T23:12:08Z")

</div>

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

[https://www.openprocessing.org/sketch/722057](https://www.openprocessing.org/sketch/722057)
