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 › MouseClicked() and loops
Page Index Toggle Pages: 1
MouseClicked() and loops? (Read 817 times)
MouseClicked() and loops?
Apr 9th, 2010, 11:03am
 
Hello all,

I'm new to Processing and I haven't done much work with object-oriented programming, so I'm trying to make a simple program using classes.

int i = 0;
Block[] blocks = new Block[10];

void setup() {
 background(0);
 stroke(255);
 size(600, 600);
}


class Block {
 int weight = 1;
 float side = 10;
 float xPos;
 float yPos;
 boolean created = false;
 
 Block(float _xPos, float _yPos) {
   xPos = _xPos;
   yPos = _yPos;
   created = true;
 }
 
 void create() {
   rect(xPos, yPos, side, side);
 }
 
 void fall() {}
   
}

void mouseClicked() {
 blocks[i].create();
 i++;
}


void draw() {
 for(int c = 0; c <= 10; c++){
   if(blocks[c].created) {
     blocks[c].fall();
   }
 }
}


It should make up to 11 blocks, adding a new one each time the user clicks. Eventually I want to expand it to add some physics to the interactions between the blocks, but as it is now absolutely nothing happens. I think this is for one of a few possible reasons:

1) I initialized the array wrong
2) The for() loop in draw() blocks the MouseClicked() function
3) I declared the class wrong

Also, a NullPointerException is raised on the if() statement every time, but I think that should be expected since it looks like the MouseClicked() function isn't running at all.

Thoughts?
Re: MouseClicked() and loops?
Reply #1 - Apr 9th, 2010, 11:11am
 
I changed it slightly and commented out the for() loop and now it works, but I'd like to extend it next to check to see how many blocks have been made. It appears that the for() loop stops MouseClicked() from running, so what is the better way to check every object of the array?

int i = 0;
Block[] blocks = new Block[10];

void setup() {
 background(0);
 stroke(255);
 size(600, 600);
}


class Block {
 int weight = 1;
 float side = 10;
 float xPos;
 float yPos;
 boolean created = false;
 
 Block(int _xPos, int _yPos) {
   xPos = _xPos;
   yPos = _yPos;
   created = true;
 }
 
 void create() {
   rect(xPos, yPos, side, side);
 }
 
 void fall() {}
   
}

void mouseClicked() {
 blocks[i] = new Block(mouseX, mouseY);
 blocks[i].create();
 i++;
}


void draw() {
//  for(int c = 0; c <= 10; c++){
//    if(blocks[c].created) {
//      blocks[c].fall();
//    }
//  }
}
Re: MouseClicked() and loops?
Reply #2 - Apr 9th, 2010, 4:41pm
 
I changed your code a bit. Check it out:


Code:
Block[] blocks = new Block[0];

void setup() {
background(0);
stroke(255);
size(600, 600);
}

class Block {
int weight = 1;
float side = 10;
float xPos;
float yPos;

Block(int _xPos, int _yPos) {
xPos = _xPos;
yPos = _yPos;
rect(xPos, yPos, side, side);
}

void drawBlock() {
rect(xPos, yPos, side, side);
}

void fall() {
}
}

void mouseClicked() {
append(blocks, new Block(mouseX, mouseY));
}

void draw() {
for(int c = 0; c < blocks.length; c++){
Block b=blocks[c];
b.drawBlock();
}
}


you had a global iterator "i" which is unnecessary. You can just find the length of your array and iterate using a local iterator (as i did in draw()).
furthermore, array items have to be explicitly initialized, otherwise they are null, and therefore contain no variables. That's why your "created" variable couldn't be seen.
the append(array, object) function is processing-specific. Generally it is not an easy (or good) practice appending stuff to arrays. You could use ArrayLists which are much more flexible. Check out the reference, it is documented there.
Page Index Toggle Pages: 1