# How do I spawn ellipses at a given time interval and is it possible to create objects infinitely until a condition has been met?

**URL:** <https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976>\
**Category:** Coding Questions\
**Created:** [September 30, 2018, 10:48am UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976 "2018-09-30T10:48:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![K-ran123](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/k-ran123/32/1776_2.png) [@K-ran123](https://discourse.processing.org/u/K-ran123)\
**Post date:** [September 30, 2018, 10:48am UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/1 "2018-09-30T10:48:35Z")

</div>

Hello,  
I am making a program similar to Aimbooster where the player has to click on the target (ellipse) before it disappears. At the moment, i got the targets (ellipses) to spawn at random locations using random() on their x and y axis. In addition, I created the expand/shrink animation of the targets.  
Currently, I am struggling to let the targets spawn at a given time interval and spawning them until, lets say, when the player loses 3 lives (when one target disappears = 1 life gone). I haven’t coded the lives/player interaction yet as you can see in my code as I am really stuck on the spawning.  
**summary** : At the moment, the program spawns 50 targets (ellipses) at the same time but I would like it to spawn at a given time interval and instead be spawned infinite amount of times (not limited to 50) until player loses all lives.  
My knowledge is very limited so, if possible, to explain it with basic syntax.  
Here’s my sketch’s code:

```auto
Target[] target;//declaring array variable

void setup(){
size(1000,1000);//canvas size
  target = new Target[50];//creates 50 new targets.
  for(int i=0;i<target.length;i++){
    target[i] = new Target();//new object
  }
  frameRate(60);
}

void draw(){
  background(0);
  for(Target targets: target){
  targets.draw();//calls draw() method from Target class
  }
}

```

My Target class code:

```auto
class Target{
boolean targetExpand; 
float x,y,radius;
color colour;
Target(){
targetExpand = true;
radius = 0;//initial radius for target
colour = 255;
x = random(1000);//random x axis position
y = random(1000);//random y axis position
}

void draw(){ 
  stroke(100);
  fill(colour);//initial target's colour
  ellipse(x,y, radius, radius);//target's location and size
  targetAnimation();//calling targetAnimation method
}
  
void targetAnimation(){
  if (targetExpand && radius<150){//targetExpand = true && radius is less than 150: increment radius
    radius++;
  }
  else{//targetExpend = false: reverse (decrement)
    targetExpand = false;
    radius--;
  }
  if (radius==0){ //If radius = 0, targetExpand = true: repeat first if statement
    targetExpand = true;
  }
}
}

```

I don’t know how to start, that’s why you won’t see any attempts…well, I did attempt several times but I deleted it since it doesn’t make sense lol.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 30, 2018, 12:22pm UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/2 "2018-09-30T12:22:44Z")

</div>

Hi K-ran123,

First you will need to change your data structure in order to have as many target as you want. Instead of using a simple array, you can use an [arrayList](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html)

So you would need to replace that:

```auto
Target[] target;

```

by that:

```auto
arrayList<Target> target;

```

Then to handle the creation of your target, we can create a dedicated method:

```auto
void spawnTarget() {
  target.add(new Target());
}

```

Now for the timing, we’ll need to store the last time a target was spawn and so speed at wich you want the target to spawn so add those variables to the beginning of the sketch: `int lastTargetSpawn` and `int spawnDeltaTime

You can now update the spawn method like so:

```auto
void spawnTarget() {
  target.add(new Target());
  lastTargetSpawn = millis();
}

```

Now in your draw function you just need to check if the time between the last spawn time and now is superior to the target time and if so spawn a target:

```auto
void draw(){
  background(0);
  if (millis() - lastTargetSpawn > spawnDeltaTime) {
    spawnTarget();
  }
}

```

It is all untested but you get the idea 🙂

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 30, 2018, 12:28pm UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/3 "2018-09-30T12:28:41Z")

</div>

You can also check this thread where @gotoloop propose a solution: [Array and object moving and not moving Coding Questions](https://discourse.processing.org/t/array-and-object-moving-and-not-moving-coding-questions/698)

---

<div class="post-metadata">

**Author:** ![K-ran123](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/k-ran123/32/1776_2.png) [@K-ran123](https://discourse.processing.org/u/K-ran123)\
**Post date:** [September 30, 2018, 12:50pm UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/4 "2018-09-30T12:50:50Z")

</div>

Hi jb4x,  
Thanks for taking your time to help me 🙂  
I temporarily commented out my for loops in my setup() and draw() methods, and added your code in.  
When i ran the program, it said NullPointerException and highlighted

> [@jb4x](#):
>
> target.add(new Target());

from the spawnTarget() method. What went wrong? I’m new to this arraylist so i’m not sure how to tackle this 😕  
I looked at the @GoToLoop solution but i prefer not to use a custom library until I learn the fundamentals lol

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 30, 2018, 12:53pm UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/5 "2018-09-30T12:53:10Z")

</div>

Ho yeah sorry, I forgot to initialize the object.

In `setup()` you should add this line:

```auto
target = new arrayList<Target>();

```

The `NullPointerException` means that you are trying to access something that does not exists.

---

<div class="post-metadata">

**Author:** ![K-ran123](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/k-ran123/32/1776_2.png) [@K-ran123](https://discourse.processing.org/u/K-ran123)\
**Post date:** [September 30, 2018, 1:05pm UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/6 "2018-09-30T13:05:24Z")

</div>

Thanks, it got rid of the error.  
However, when I run the sketch, nothing spawns.  
Do i need to set a value for lastTargetSpawn or spawnDeltaTime variable?  
Sorry if I may seen as being spoon fed, I just can’t wrap myself around with programming…

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 30, 2018, 1:17pm UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/7 "2018-09-30T13:17:15Z")

</div>

Well you still need to draw them:

```auto
for (int i = 0; i < target.size(); i++) {
  target.get[i].draw();
}

```

And to be complete you need to first check if there is something to draw:

```auto
if (target.size() > 0) {
  for (int i = 0; i < target.size(); i++) {
    target.get[i].draw();
  }
}

```

---

<div class="post-metadata">

**Author:** ![K-ran123](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/k-ran123/32/1776_2.png) [@K-ran123](https://discourse.processing.org/u/K-ran123)\
**Post date:** [September 30, 2018, 1:38pm UTC](https://discourse.processing.org/t/how-do-i-spawn-ellipses-at-a-given-time-interval-and-is-it-possible-to-create-objects-infinitely-until-a-condition-has-been-met/3976/8 "2018-09-30T13:38:00Z")

</div>

Thank you so much! 😌 I guess I need revise on arraylist and the structure of a sketch.
