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 & HelpSyntax Questions › Objects and voids and errors, oi vey.
Page Index Toggle Pages: 1
Objects and voids and errors, oi vey. (Read 555 times)
Objects and voids and errors, oi vey.
Dec 7th, 2009, 9:45am
 
Hello Everyone,
I seem to be having issues with my code, as I am getting "unexpected token: void", at "void setup(){" . I was hoping that the community could be so kind as to give me a fresh set of eyes and help me with this debugging issue. I apologize for my n00biness, but everyone has to start some where, so any help any one is willing to give will be highly appreciated.  
Thank you all for your time!

Block1 = new Block1(25,10);
Block2 = new Block2(50,10);

void setup(){
 size(200,200);
 background(0);
 rectMode(CENTER);
}
void draw(){
 String[] blocks = {"1","2"};
 int index = int(random(blocks.length));
 println(blocks[index]);
 if (index=1){
   Block1.display;
 }
 if (index=2){
   Block2.display;
 }
}

class Block1 {
 rectMode(CENTER);
 int blockX, blockY;
 Block1 (int x, int y){
   blockX=x;
   blocyY=y;
 }
 
 void display(){
   fill(255,0,0);
   rect(blockX,blockY,10,10);
 }
}

class Block2{
 rectMode(CENTER);
 int blockX, blockY;
 Block2 (int x, int y){
   blockX = x;
   blocyY = y;
 }
 void display(){
   fill(0,255,0);
   rect(blockX,blockY,10,10);
 }
}
Re: Objects and voids and errors, oi vey.
Reply #1 - Dec 7th, 2009, 10:48am
 
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();
}
Re: Objects and voids and errors, oi vey.
Reply #2 - Dec 7th, 2009, 12:57pm
 
AH, I see my problem.
Thank you so much, I appreciate your help.
Page Index Toggle Pages: 1