I need help to spawn multiple objects at random moments

int grid = 15;
Player player;
Enemy enemy;
float posx;
float posy;

void setup(){
  size(1200,800);
  player = new Player();
  frameRate(20);
  enemy = new Enemy();
}
  
  void draw(){
    background(0);
    player.update();
    player.show();
    enemy.Display();
    enemy.Update(posx, posy);
  }

class Enemy {
  
  PVector pos;
  PVector vel;
  PVector acc;
  
 Enemy (){
   
   pos = new PVector (width / 2, height / 2);
   vel = new PVector (0,0);
   acc = new PVector (0,0);
 }
   
   
   void Display (){
     
   fill(0,255,0);
   ellipse (pos.x, pos.y, 30,30);

   
   }
   void Update(float x, float y){
   PVector player = new PVector(x, y);
   PVector dir = PVector.sub (player,pos);
   
   dir.normalize();
   
   acc = dir;
   
   vel.add(acc);
   vel.limit(4);
   pos.add(vel);
   
   }
}




void update(){
  hist.add(pos.copy());
  pos.add(mov);
  pos.x += mov.x*grid;
  pos.y += mov.y*grid;

//loop boarders
    pos.x = (pos.x + width) % width;
    pos.y = (pos.y + height) % height;
    
   if (hist.size() > len) {
      hist.remove(0);
    }
}

void show() {
    noStroke();
    fill(255,1,1);
    rect(pos.x, pos.y, grid, grid);
    for (PVector p : hist) {
      rect(p.x, p.y, grid, grid);
    }
    posx = pos.x;
    posy = pos.y;
}

}

//movement
void keyPressed(){
  if (keyCode == LEFT) {
    player.mov.x = -1;
    player.mov.y = 0;
  } else if (keyCode == RIGHT) {
    player.mov.x = 1;
    player.mov.y = 0;
  } else if (keyCode == UP) {
    player.mov.y = -1;
    player.mov.x = 0;
  } else if (keyCode == DOWN) {
    player.mov.y = 1;
    player.mov.x = 0;
  }
}

void keyReleased(){
if (keyCode == LEFT) {
    player.mov.x = 0;
    } else if (keyCode == RIGHT) {
    player.mov.x = 0;
    } else if (keyCode == UP) {
    player.mov.y = -0;
     } else if (keyCode == DOWN) {
    player.mov.y = 0;
}
}

So my question is: Which “commands” should i apply to make it happen

1 Like

Try to take this as an inspiration:

Code
ArrayList<ci> circles = new ArrayList<ci>();
void setup() {
  size(600, 600);
}
void draw() {
  if(random(100) > 99) {
    int num = floor(random(2,6));
    for(int i = 0; i < num; i++) {
      circles.add(new ci(random(width),random(height),random(10,20)));
    }
  }
  for(ci c : circles) {
    c.display();
  }
}

class ci {
  float x, y, r;
  ci(float X, float Y, float R) {
    x = X;
    y = Y;
    r = R;
  }
  void display() {
    fill(255);
    circle(x,y,r*2);
  }
}

In short, every frame there is a 1% chance of spawning between 2 and 6 circles. Adding ‘enemies’ would probably be the easiest using ArrayLists since they can easily be expanded.

Maybe something like that would work?

Looking back at it now, I found some things I should explain. I am not sure of your current level, so I’ll begin with the basics.

First of all, you need somewhere to store the objects. The simplest way to do it is to use arrays. Arrays are essentially containers for whatever you want. They are easy to create and easy to modify.

A tip I found really useful is this:
The program does not distinguish between a variable and an array item. So array[0] would be treated the same as any other variable.

Some reference pages:

Secondly, you need a way to modify them. I suggest checking the video by the coding train

After you watched this video and a few of his tutorials I am sure you could understand it!

Good luck!

1 Like