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.
IndexProgramming Questions & HelpPrograms › Many bouncing ball sketch problems
Page Index Toggle Pages: 1
Many bouncing ball sketch problems (Read 529 times)
Many bouncing ball sketch problems
Dec 12th, 2008, 4:11pm
 
I am doing the many bouncing ball sketch from the "creative coding and comp art" book,  I think i got it all right but i cant run it it says it cannot find nothing named "i"  altough i think i'ts there , here's the code.

Thanks !

Code:

// many bouncing bal program

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


size (400,400);
background(0);

//initialize values for all balls

for (int i=0; i<ballCount; i++)

// set variable 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);
}
}
// turnoff stroke

noStroke();
frameRate(30);


// draw

void draw(){

// updates background

beackground(0);

for(int i=0; i<ballCount; i++){

fill (i*255/ballCount);
//drawballs

ellipse(xpos[i],ypos[i], wdth[i],ht[i]);

// upgrade ball pos
xpos[i]+=xspeed[i];
ypos[i]+=yspeed[i];

// conditionals
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;
}
}
}
Re: Many bouncing ball sketch problems
Reply #1 - Dec 12th, 2008, 5:32pm
 
In setup(), the for loop has no opening brace, so only the first line (xspeed) knows i (scoped to the loop).

I think the noStroke and frameRate calls should be in setup() too.
Re: Many bouncing ball sketch problems
Reply #2 - Dec 12th, 2008, 6:27pm
 
thanks , but i dont really understand what you mean, i see one brace  in the for loop, could you explain or copy , modify and paste the code please ? thanks !

...
Re: Many bouncing ball sketch problems
Reply #3 - Dec 12th, 2008, 6:58pm
 
Problem of vocabulary: for me (and many people)
- ( is parenthesis
- { is brace (also called soft brace)
- [ is square brace
- < is angle brace

Happy
I know the terminology varies with culture, country and people.

You should put a (soft) brace { at the location where you have the caret in your screenshot.
Re: Many bouncing ball sketch problems
Reply #4 - Dec 12th, 2008, 7:47pm
 
thanx , i tried but it still wont run,
i also tried puting a closed curly brace at the end and many other ways but just cant get it.

...


Have you tried running the sketch ? could you post the solution please ?thanks !
Re: Many bouncing ball sketch problems
Reply #5 - Dec 12th, 2008, 8:12pm
 
Curly brace is where you place the code that you want repeated in a for statement.

A didactic code:



for(int i = 0; i < 10; i++){

   println( "I am at:"+i);

}




// and also, just to show that spaces and returns are just
// decorations to make it easier for you to read the code:

for(int i = 0; i < 10; i++){ println( "I am at:"+i); }

// above is exactly the same code.



// 10 is where you place your ballcount variable.

// as PhiLho said, please note the differences between
// the type of parenthesis/brackets.
Re: Many bouncing ball sketch problems
Reply #6 - Dec 12th, 2008, 8:18pm
 
Or you can say bracket instead of brace.
Re: Many bouncing ball sketch problems
Reply #7 - Dec 12th, 2008, 8:57pm
 
man i am ver thankfull for all your help , but look i tried allready many ways and i just cant  get it , could you please post the right code.

...

thanks !
Re: Many bouncing ball sketch problems
Reply #8 - Dec 12th, 2008, 9:46pm
 
OK, here is the working code, look at my //--> remarks...
Code:
// many bouncing bal program  

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


size (400,400);
background(0);

//--> I moved that up, as said
// turnoff stroke

noStroke();
frameRate(30);

//initialize values for all balls

for (int i=0; i<ballCount; i++)
{ //--> I added this curly/soft brace
// set variable ball speed

xspeed[i] = random(1, ballSpeed);
yspeed[i] = random (-ballSpeed, ballSpeed);

//ball varied ball sizes

wdth[i] = random(1, ballSize); //--> Also corrected here, case is important
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);
}
}


// draw

void draw(){

// updates background

background(0); //--> Fixed a typo here!

for(int i=0; i<ballCount; i++){

fill (i*255/ballCount);
//drawballs

ellipse(xpos[i],ypos[i], wdth[i],ht[i]);

// upgrade ball pos
xpos[i]+=xspeed[i];
ypos[i]+=yspeed[i];

// conditionals
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;
}
}
}
Re: Many bouncing ball sketch problems
Reply #9 - Dec 12th, 2008, 10:33pm
 
Thank you so much !

i would never see all those things !!!

8D
Re: Many bouncing ball sketch problems
Reply #10 - Dec 13th, 2008, 2:10am
 
what should i do to get all the balls white?

i dont understand where the code tells the balls to be one color .

thanks !
Re: Many bouncing ball sketch problems
Reply #11 - Dec 13th, 2008, 2:11am
 
i ogt it , 255 in the fill option.


but how to make it a random alpha value , but only in whites ?
Re: Many bouncing ball sketch problems
Reply #12 - Dec 13th, 2008, 2:19am
 
TweakingKnobs wrote on Dec 13th, 2008, 2:11am:
i ogt it , 255 in the fill option.


but how to make it a random alpha value , but only in whites


fill(color(255,255,255,random(255)));
Page Index Toggle Pages: 1