Please use the [ code ] and [ /code ] tags (without spaces) or
Copy for discourse option in the PDE : it's easier to read, then!
Your declaration is bad :
Code:// next line is bad:
Block1 = new Block1(25,10);
// next line is good :
Block1 yourVariableName = new Block1(25,10);
and you should just declare the variable out of
setup(), and set it up in
setup :
Code:Block myBlock;
void setup() {
myBlock = new Block(10, 10);
}
Your are using classes, but you don't use the power of OOP : since each Block has a
draw() method and you try to iterate through the different blocks, your two classes should extend one "mother" class :
Code:class Block {
// put here what the classes have in common, like:
int x, y;
void draw() {}
}
class Block1 extends Block {
// put specific methods and parameters here, like :
void display {
fill(255, 0, 0);
rectMode(CORNER);
rect(x, y, 10, 10);
}
}
class Block 2 extends Block {
// put specific methods and parameters here, like :
void display {
fill(0, 255, 0);
rectMode(CORNER);
rect(x, y, 10, 10);
}
}
so you can iterate through a Block[] array, and you don't need your tricky step to draw either block1, either block2.
Code:Block[] blocks;
void setup() {
blocks = new Blocks[2];
blocks[0] = new Block1(10, 10);
blocks[1] = new Block2(20, 30);
}
void draw() {
blocks[0].draw();
blocks[1].draw();
}