# I need help to spawn multiple objects at random moments

**URL:** https://discourse.processing.org/t/i-need-help-to-spawn-multiple-objects-at-random-moments/32796
**Category:** Coding Questions
**Created:** [October 13, 2021, 7:14am UTC](https://discourse.processing.org/t/i-need-help-to-spawn-multiple-objects-at-random-moments/32796 "2021-10-13T07:14:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![YP1702](https://avatars.discourse-cdn.com/v4/letter/y/ea666f/32.png) [@YP1702](https://discourse.processing.org/u/YP1702)
#### Post date: [October 13, 2021, 7:14am UTC](https://discourse.processing.org/t/i-need-help-to-spawn-multiple-objects-at-random-moments/32796/1 "2021-10-13T07:14:17Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![YP1702](https://avatars.discourse-cdn.com/v4/letter/y/ea666f/32.png) [@YP1702](https://discourse.processing.org/u/YP1702)
#### Post date: [October 13, 2021, 7:14am UTC](https://discourse.processing.org/t/i-need-help-to-spawn-multiple-objects-at-random-moments/32796/2 "2021-10-13T07:14:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [October 13, 2021, 7:32pm UTC](https://discourse.processing.org/t/i-need-help-to-spawn-multiple-objects-at-random-moments/32796/3 "2021-10-13T19:32:58Z")

</div>

Try to take this as an inspiration:

> **Code**
>
> ```auto
> 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?

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [October 13, 2021, 7:45pm UTC](https://discourse.processing.org/t/i-need-help-to-spawn-multiple-objects-at-random-moments/32796/4 "2021-10-13T19:45:51Z")

</div>

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:

> **[Array / Reference](https://processing.org/reference/array)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

> **[ArrayList / Reference](https://processing.org/reference/arraylist)**
>
> An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is…

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

[![](https://img.youtube.com/vi/KkyIDI6rQJI/maxresdefault.jpg "Coding Challenge #4: Purple Rain in Processing") ](https://www.youtube.com/watch?v=KkyIDI6rQJI)

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

Good luck!
