# How to make another ball after a click

**URL:** <https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038>\
**Category:** Coding Questions\
**Created:** [October 24, 2021, 11:11am UTC](https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038 "2021-10-24T11:11:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenga123456](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kenga123456](https://discourse.processing.org/u/Kenga123456)\
**Post date:** [October 24, 2021, 11:11am UTC](https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038/1 "2021-10-24T11:11:50Z")

</div>

```auto
class Ball {
	float x = random(0, width);
	float y = 100;
  float ySpeed = 1;

void move() {
	y += ySpeed
	
}
void show() {
ellipse(x, y, 20, 20);
	
}

}
Ball[] balls = new Ball [1];

void setup() {
	frameRate(60);
	size(640, 360);
	
	for (int i = 0; i < balls.length; i++;) {
		balls[i] = new Ball();
	}

}

void draw() {
	background(100);
	for (int i = 0; i < balls.length; i++) {
		balls[i].show();
		balls[i].move();
	}
	if (mousePressed) {
//dont know what code I can add here or if I have to do something else
	}
	
}

```

---

<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:** [October 24, 2021, 11:27am UTC](https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038/2 "2021-10-24T11:27:34Z")

</div>

something like balls=  
(Balls[]) append(balls, new Ball(….));

Check reference for append

Alternatively check Arraylist

---

<div class="post-metadata">

**Author:** ![Kenga123456](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kenga123456](https://discourse.processing.org/u/Kenga123456)\
**Post date:** [October 24, 2021, 12:31pm UTC](https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038/3 "2021-10-24T12:31:50Z")

</div>

I tried but I don’t understand.  
If I use Arraylist do I have to change my entire code?

---

<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:** [October 24, 2021, 12:34pm UTC](https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038/4 "2021-10-24T12:34:55Z")

</div>

What don’t you understand

Just read the reference and type my line…

---

<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:** [October 24, 2021, 12:35pm UTC](https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038/5 "2021-10-24T12:35:58Z")

</div>

For ArrayList you only change a few lines

Read the reference about arraylist but array will do for now

---

<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:** [October 24, 2021, 1:06pm UTC](https://discourse.processing.org/t/how-to-make-another-ball-after-a-click/33038/6 "2021-10-24T13:06:26Z")

</div>

Hi @Kenga123456,

I would advise you to use an ArrayList as it is easy to add or delete elements from it.  
Please check this example to get a feel of how to use it and add new elements after a mouse click event:

```auto
ArrayList <PVector> balls;

void setup() {
  size(1000, 600);
  fill(230);
  noStroke();
  
  balls = new ArrayList<PVector>();
}

void draw() {
  background(20);
  
  for (int i = 0; i < balls.size(); i++) {
    ellipse(balls.get(i).x, balls.get(i).y, 20, 20);
  }
}

void mousePressed() {
  balls.add(new PVector(mouseX, mouseY));
}

```
