FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Responsive Form, Games
(Moderator: REAS)
   More Brownian Stuff
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: More Brownian Stuff  (Read 994 times)
gkoshra

WWW Email
More Brownian Stuff
« on: Jun 3rd, 2003, 11:00am »

Hello,
I've been trying figuring out classes, and I like them. So I re-made an old program using them. You can see it here -> http://gomako.co.uk/p5/brownies/
 
I added a bit so that you could clear areas of the screen for the 'brownies' to re-conquer. I thought it'd be relatively simple, but if you try and draw for a while, it starts doing all kinds of crazy stuff, then crahses with an 'ArrayIndexOutOfBounds Exception: -1'
 
It highlights boofs[i].draw(); when it crashes.
 
Here's the code...
Code:

// Mucking about with classes and brownies
 
int w = 600;  //  width
int h = 400;  //  height
int numBoofs = 2000;  //  how many boofs - or brownies, whatever
 
Boof[] boofs = new Boof[numBoofs];  //  sets up array
 
void setup() {
  size(w, h);
  noBackground();
  ellipseMode(CENTER_RADIUS);
  myRefresh();
   
  // creates the objects
  for(int i=0; i<numBoofs; i++) {
    boofs[i] = new Boof();
  }
}
 
void loop() {
 
  for(int i=0; i<numBoofs; i++) {
    boofs[i].update();
    boofs[i].draw();
  }
 
}
 
/////////////////////////////////////////
/////////////////////////////////////////
 
class Boof
{
 
  int r = int(random(153,174));
  int g = int(random(134,176));
  int b = int(random(51,85));
 
  int xpos = int(random(width));
  int ypos = int(random(height));
 
  void update()
  {
    stroke(r,g,b);
    xpos+=(int)random(-2,2);
    ypos+=(int)random(-2,2);
    if(ypos<0 || ypos>h)
    ypos = int(random(height));
    if(xpos<0 || xpos>w)
    xpos = int(random(width));
  }
 
  void draw()
  {
    point(xpos,ypos);
  }
 
}
 
void myRefresh(){
  fill(230, 230, 200);
  rect(0,0,w,h);
  noFill();
}
 
 
void mouseDragged(){
fill(230, 230, 200);
noStroke();
ellipse(mouseX,mouseY,20,20);
noFill();
}
 

 
Any ideas? I can't see what's going on. Is it something to do with me turning stroke/fill on and off al the time?
 
Cheers,
Ben.
 
Pages: 1 

« Previous topic | Next topic »