# How to make a rectangle spawn when mouseclicked and will bounce

**URL:** <https://discourse.processing.org/t/how-to-make-a-rectangle-spawn-when-mouseclicked-and-will-bounce/25742>\
**Category:** Coding Questions\
**Created:** [November 27, 2020, 1:00am UTC](https://discourse.processing.org/t/how-to-make-a-rectangle-spawn-when-mouseclicked-and-will-bounce/25742 "2020-11-27T01:00:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![0w3n928](https://avatars.discourse-cdn.com/v4/letter/0/b77776/32.png) [@0w3n928](https://discourse.processing.org/u/0w3n928)\
**Post date:** [November 27, 2020, 1:00am UTC](https://discourse.processing.org/t/how-to-make-a-rectangle-spawn-when-mouseclicked-and-will-bounce/25742/1 "2020-11-27T01:00:02Z")

</div>

I’m trying to make another rectangle spawn when mouse is clicked once and it will collide to my first bouncing rectangle and I can add however many i want but my code doesn’t even need me to click and it doesn’t really work.

float rectx,recty,xspeed=7,yspeed=7;

void setup() {  
fullScreen();

}  
void draw() {  
background(0);  
rect(rectx,recty,300,150);  
fill(0,255,0);  
if(mousePressed==true);  
rect(recty,rectx,300,150);

if(sideCollision(rectx)) {  
xspeed = xspeed \* -1;  
}  
if(topOrBottomCollision(recty)) {  
yspeed = yspeed\*-1;  
}  
rectx = rectx + xspeed;  
recty = recty + yspeed;  
}

boolean sideCollision(float x) {  
if(x\<0 || x\>width-300) {  
return true;  
} else {  
return false;  
}  
}

boolean topOrBottomCollision(float y) {  
if(y\<0 || y\>height-150) {  
return true;  
} else {  
return false;  
}  
}

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 27, 2020, 1:55pm UTC](https://discourse.processing.org/t/how-to-make-a-rectangle-spawn-when-mouseclicked-and-will-bounce/25742/2 "2020-11-27T13:55:55Z")

</div>

you have an extra semi colon at the end of

```auto
if(mousePressed==true);

```

which is likely causing problems

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 28, 2020, 6:51pm UTC](https://discourse.processing.org/t/how-to-make-a-rectangle-spawn-when-mouseclicked-and-will-bounce/25742/3 "2020-11-28T18:51:13Z")

</div>

See here a tutorial about multiple objects bouncing. Just remove gravity.

> **[BouncyBubbles \\ Examples \\ Processing.org](https://processing.org/examples/bouncybubbles.html)**
