# How to spawn a rectangle when mouse is clicked (however many times you want) and make it do the same functions as my code can

**URL:** <https://discourse.processing.org/t/how-to-spawn-a-rectangle-when-mouse-is-clicked-however-many-times-you-want-and-make-it-do-the-same-functions-as-my-code-can/25820>\
**Category:** Coding Questions\
**Created:** [November 30, 2020, 3:56am UTC](https://discourse.processing.org/t/how-to-spawn-a-rectangle-when-mouse-is-clicked-however-many-times-you-want-and-make-it-do-the-same-functions-as-my-code-can/25820 "2020-11-30T03:56:07Z")\
**Posts on this page:** 2\
**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 30, 2020, 3:56am UTC](https://discourse.processing.org/t/how-to-spawn-a-rectangle-when-mouse-is-clicked-however-many-times-you-want-and-make-it-do-the-same-functions-as-my-code-can/25820/1 "2020-11-30T03:56:07Z")

</div>

Code:  
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:** ![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:** [November 30, 2020, 8:35am UTC](https://discourse.processing.org/t/how-to-spawn-a-rectangle-when-mouse-is-clicked-however-many-times-you-want-and-make-it-do-the-same-functions-as-my-code-can/25820/2 "2020-11-30T08:35:07Z")

</div>

You can look at ArrayList in the reference and then for loop over the items to display and to manage them

You can have one ArrayList for x and one for y

Later you can use a class Ball
