# Make balls bounce

**URL:** <https://discourse.processing.org/t/make-balls-bounce/16441>\
**Category:** Coding Questions\
**Created:** [December 16, 2019, 2:52am UTC](https://discourse.processing.org/t/make-balls-bounce/16441 "2019-12-16T02:52:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahaa1](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sahaa1](https://discourse.processing.org/u/sahaa1)\
**Post date:** [December 16, 2019, 2:52am UTC](https://discourse.processing.org/t/make-balls-bounce/16441/1 "2019-12-16T02:52:25Z")

</div>

I am trying to make a project in which when a ball bounces against the edge of the screen, another ball is produced, and I will loop it. However, due to an error which seems almost impossible to fix, the second ball is not being produced. What should I do to improve my code?

Here is my code:

```auto
void setup(){
  size(1950,1050);
  float colour1, colour2, colour3;
  float ballsize = 50.0, ballx,bally, xspeed=5, yspeed=8;
}
void draw() {
  background(0);}
 
 
void generate() {
    fill(colour1, colour2, colour3);
    ellipse(ballx, bally, ballsize, ballsize);
}

void changeColour() {
    colour1 = random(255);
    colour2 = random(255);
    colour3 = random(255);
    generate(); 
}

void myball(){
  myball= new ball(ballx, bally, ballsize, ballsize);
  myball.createBall();
  myball.generate();
}

class Ball{
  generate(){
  createBall();{
  ellipse(ballx, bally, ballsize, ballsize); 

  if(side_collision(ballx)) {
    xspeed = xspeed*-1;
    ellipse(ballx, bally, ballsize, ballsize);
    changeColour()
    myball.create(){
    myball.generate(){
 new ellipse(ballx-xspeed, bally-yspeed, ballsize, ballsize);
  }
    }
    }
  }
  
  
  if(toporbottom_collision(bally)) {
    yspeed = yspeed*-1;
    ellipse(ballx, bally, ballsize, ballsize);
  changeColour();
  }
     {
 ballx = ballx +xspeed;
  bally = bally +yspeed;
}

boolean side_collision(float x) {
  if(x<0 || x>width) {
    return true;
  } else {
    return false;
  }
}

boolean toporbottom_collision(float y) {
  if (y<0 || y>height){
    return true;
  } else {
    return false;
  }
  
}

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 16, 2019, 3:36am UTC](https://discourse.processing.org/t/make-balls-bounce/16441/2 "2019-12-16T03:36:23Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 16, 2019, 7:16am UTC](https://discourse.processing.org/t/make-balls-bounce/16441/3 "2019-12-16T07:16:53Z")

</div>

First of all… what languague is that?? It doesn‘t seem like Java, nor P5.js or py… Also, what is your class Ball supposed to do? Especially the beginning seems not like any valid code syntax (at least none i know…). Please add some information and format your Code.

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [December 16, 2019, 12:40pm UTC](https://discourse.processing.org/t/make-balls-bounce/16441/4 "2019-12-16T12:40:59Z")

</div>

I think it will help to study the Object tutorial: [https://processing.org/tutorials/objects/](https://processing.org/tutorials/objects/).

Once you’ve read it, I recommend to start a new sketch and start out really simple. Only try to display a ball on your screen by using a class. It doesn’t need to be moving, nor have random colours; just make sure your class is working. Once your class works, then you could add more methods such as movement and bouncing.

---

<div class="post-metadata">

**Author:** ![Sonarmiss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sonarmiss/32/7594_2.png) [@Sonarmiss](https://discourse.processing.org/u/Sonarmiss)\
**Post date:** [December 24, 2019, 2:05pm UTC](https://discourse.processing.org/t/make-balls-bounce/16441/5 "2019-12-24T14:05:17Z")

</div>

Hi there! Would be happy to help you, however, got really confused as I couldn’t really understand, what language you’re using… Could you specify that one, please?
