We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexSuggestions & BugsWebsite,  Documentation,  Book Bugs › Error in "Many Bouncing Balls"
Page Index Toggle Pages: 1
Error in "Many Bouncing Balls" (Read 2499 times)
Error in "Many Bouncing Balls"
Apr 14th, 2010, 1:07am
 
My first post here, but Oh my god Processing feels like its made for me :-)

I cant make the "Many Bouncing Balls" program in the book "Processing - Creative Coding and Computational Art" work :-(

Can you spot, what the typo/mistake is?

Quote:
/*
title: Many Bouncing Balls
description: balls deflect off sketch window edges
created: August 9, 2005
by: Ira Greenberg
*/
// global variables
int ballCount = 500;
int ballSize = 8;
int ballSpeed = 3;
float[]xspeed = new float[ballCount];
float[]yspeed= new float[ballCount];
float[]xpos = new float[ballCount];
float[]ypos = new float[ballCount];
float[]wdth = new float[ballCount];
float[]ht = new float[ballCount];
//initialize sketch
void setup(){
//set sketch window size and background color
size(400, 400);
background(0);
//initialize values for all balls
for (int i=0; i<ballCount; i++)
// set varied ball speed
xspeed[i] = random(1, ballSpeed);
yspeed[i] = random(-ballSpeed, ballSpeed);
// ball varied ball sizes
wdth[i]= random(1, ballSize);
ht[i]= wdth[i];
// set initial ball placement
xpos[i] = width/2+random(-width/3, width/3);
ypos[i] = height/2+random(-height/3, height/3);
}
// turn off shape stroke rendering
noStroke();
//set the animation loop speed
frameRate(30);
}
// begin animation loop
void draw(){
/*updates background
comment out to use alternate
fill option below*/
background(0);
for (int i=0; i<ballCount; i++){
  /*To use this fill option:
1. uncomment fill call below
2. comment out the background
function call above*/
// fill(i*255/ballCount);
//draw balls
ellipse(xpos[i], ypos[i], wdth[i], ht[i]);
//upgrade position values
xpos[i]+=xspeed[i];
ypos[i]+=yspeed[i];
/*conditionals:
detects ball collision with sketch window edges
accounting for ball thickness.
*/
if (xpos[i]+wdth[i]/2>=width || xpos[i]<=wdth[i]/2){
xspeed[i]*=-1;
}
if (ypos[i]+ht[i]/2>=height || ypos[i]<=ht[i]/2){
yspeed[i]*=-1;
}
}
}



Edit: The error message I get is:

unexpected token: ;

on this line

Quote:
noStroke();

Re: Error in "Many Bouncing Balls"
Reply #1 - Apr 14th, 2010, 2:08am
 
From a quick glance, I think you forgot the opening brace on the for () loop line.
Not too sure since you neglected to type the indentations... Wink
Re: Error in "Many Bouncing Balls"
Reply #2 - Apr 14th, 2010, 2:51am
 
I think you missed an an opening "{" in the previous loop. Angry
ie set ball speed..........................
Re: Error in "Many Bouncing Balls"
Reply #3 - Apr 19th, 2010, 11:13am
 
Yes, this is a typo in the book.

Line 23 should be:
for (int i=0; i<ballCount; i++){

Ira
Re: Error in "Many Bouncing Balls"
Reply #4 - Apr 28th, 2010, 12:01am
 
Thx for the response Ira.

Your book is GREAT by the way.

Cheers Thomas from Denmark
Page Index Toggle Pages: 1