Trouble with the bouncing ball example from the NoC book

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 ,

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

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

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

you have left:

  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 (){}

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

so my version of this whole code would look like

// 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)

2 Likes