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

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:

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:

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.

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

So you would need to replace that:

Target[] target;

by that:

arrayList<Target> target;

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

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:

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:

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

It is all untested but you get the idea :slight_smile:

1 Like

You can also check this thread where @gotoloop propose a solution: Array and object moving and not moving Coding Questions

1 Like

Hi jb4x,
Thanks for taking your time to help me :slightly_smiling_face:
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

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

Ho yeah sorry, I forgot to initialize the object.

In setup() you should add this line:

target = new arrayList<Target>();

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

1 Like

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…

Well you still need to draw them:

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:

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

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