# Trouble with the bouncing ball example from the NoC book

**URL:** <https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705>\
**Category:** Coding Questions\
**Created:** [August 30, 2019, 2:19pm UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705 "2019-08-30T14:19:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyml](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@cyml](https://discourse.processing.org/u/cyml)\
**Post date:** [August 30, 2019, 2:19pm UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/1 "2019-08-30T14:19:55Z")

</div>

on my first embark of the first example in The Nature of Code, Processing is telling me

> Missing right parenthesis “)”

and I compared my code to Daniel’s sample code… and I couldn’t figure out why. Can someone help me, please???

Thank you!!!

 ![19](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4cdabf1c8b0cf8043cf1b54da87593d41d654fee.png)

---

<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:** [August 31, 2019, 12:27am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/2 "2019-08-31T00:27:58Z")

</div>

picture can be very helpful,  
but for test might need code,  
or here better just link to where you get it from  
[https://github.com/nature-of-code/noc-examples-processing/blob/master/chp01\_vectors/NOC\_1\_1\_bouncingball\_novectors/NOC\_1\_1\_bouncingball\_novectors.pde](https://github.com/nature-of-code/noc-examples-processing/blob/master/chp01_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pde) ,

and that version is working here/  
same like the right side of your picture.

now for the left side / the problem /  
again original right:

```auto
  if ((x > width) || (x < 0)) {
    xspeed = xspeed * -1;
  }
  if ((y > height) || (y < 0)) {
    yspeed = yspeed * -1;
  }

```

you have left:

```auto
  if ((x > width || (x < 0)) {
    xspeed = xspeed * -1;
  }
  if ((y > height || (y < 0)) {
    yspeed = yspeed * -1;
  }

```

so you deleted 2 times a ‘)’  
and the red error info ( in the editor and in the console header line )  
says it all.

also please see that the editor has that great feature that it can indicate code blocks  
{ } or ( )  
by posting the cursor ( ? by mouse click ?) behind one see `(|` and see the corresponding as `[)]`

* * *

now you can choose between many coding styles and formatting,  
but one point should be to test if your free form code format  
will survive using the auto format [ctrl][t]

i like following style / for me more easy readable without UNNEEDED (){}

```auto
  if ( x > width || x < 0 ) xspeed *= -1;
  if ( y > height || y < 0 ) yspeed *= -1;

```

so my version of this whole code would look like

```auto
// The Nature of Code / Daniel Shiffman / http://natureofcode.com
// Example 1-1: Bouncing Ball, no vectors

float x = 100, y = 10, xspeed = 2.5, yspeed = 2;

void setup() {
  size(800, 200);
  stroke(0);
  strokeWeight(2);
  fill(127);
}

void draw() {
  background(255);
  if ( ( x += xspeed ) > width || x < 0 ) xspeed *= -1;
  if ( ( y += yspeed ) > height || y < 0 ) yspeed *= -1;
  ellipse(x, y, 48, 48);
}

```

does that look more snappy?

and short is good HA,  
BUT what we should NOT delete is the info where we take it from  
even if would not only legally be required, the book is CC BY NC 3.0  
still for respect keep the AUTHORs name in your code.

( your very first action as a beginner was to delete the first 4 lines of the tutorials code)

---

<div class="post-metadata">

**Author:** ![Marinus](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marinus](https://discourse.processing.org/u/Marinus)\
**Post date:** [August 31, 2019, 9:18am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/3 "2019-08-31T09:18:42Z")

</div>

I agree with your formatting being a very clean code for smaller files as well and more oversight. But, if someone is a beginner I think it is important to write out every variable separately in the global floats and create the if/for/while statements on separate lines.

This is just for the learning curve. It’s the same as when they teach children English and they make them write out the complete words as in: it’s = it is, he’s = he is, won’t = will not etc etc etc.

Learning the basic way will help staying out of trouble at a later stage.

That said, it’s a great book that I have just gotten myself and will start working with soon. It may well be the best book I have got for Processing so far. There are many but it’s hard to find good ones for things like generative art etc.

Just to emphasize, these is what’s missing in your code but it’s been explained really well by @kll already:  
 ![57](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0d91179fcbd1c3376aab1c74aef0ae513a4c89d1.png)

ps I bookmarked your post myself because I was actually looking for a way to condense my code! so thanks 🙂

---

<div class="post-metadata">

**Author:** ![cyml](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@cyml](https://discourse.processing.org/u/cyml)\
**Post date:** [August 31, 2019, 9:30am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/4 "2019-08-31T09:30:20Z")

</div>

hi kll, thank you very much for the reply. I was typing out the code by following the code from the kindle book but found the book missed out plus and minus. Then I went finding the code from github.

I will keep the credit in the code in the future, thank you for clarifying that.

now I see the missing parenthesis, gosh I was blind. 😂  
And no idea about the formatting style.

---

<div class="post-metadata">

**Author:** ![cyml](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@cyml](https://discourse.processing.org/u/cyml)\
**Post date:** [August 31, 2019, 9:31am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/5 "2019-08-31T09:31:25Z")

</div>

yes I finally saw it. it was so obvious now i read it myself.

Thank you guys!!

---

<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:** [August 31, 2019, 9:34am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/6 "2019-08-31T09:34:54Z")

</div>

possibly i am more the visual type, ( and also like excel lists )  
but what i found for me is / if i not can simplify / delete ‘((’ /

```auto
 if ( (x > width) || (x < 0) ) {
// looks better as
 if ((x > width) || (x < 0)) {

```

what you say?

---

<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:** [August 31, 2019, 9:37am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/7 "2019-08-31T09:37:17Z")

</div>

> [@kll](#):
>
> if ((x \> width) || (x \< 0)) {

same as :

if (x \> width || x \< 0) {

---

<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:** [August 31, 2019, 9:41am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/8 "2019-08-31T09:41:50Z")

</div>

thanks,  
we had that here [[question] help! trouble with the bouncing ball example from the NoC book](https://discourse.processing.org/t/question-help-trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/2) already.

just wanted to say the `( (` spacing can help me AND  
is not killed by [ctrl][t]

---

<div class="post-metadata">

**Author:** ![Marinus](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marinus](https://discourse.processing.org/u/Marinus)\
**Post date:** [August 31, 2019, 9:50am UTC](https://discourse.processing.org/t/trouble-with-the-bouncing-ball-example-from-the-noc-book/13705/9 "2019-08-31T09:50:30Z")

</div>

By the way @cyml is on a mac so it would be [cmd]+[t] 😉
