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 › Class help Constructor undefined
Page Index Toggle Pages: 1
Class help? Constructor undefined (Read 658 times)
Class help? Constructor undefined
Nov 14th, 2009, 10:44am
 
Hey, I'm sort of new to processing but I'm trying to get an understanding of how to use constructors and classes.  For a fun project, I'm trying to make a little tank that will drive around with "WASD" keys and move its cannon with the mouse.  I've gotten pretty far but I'm just stuck!  Can anyone help me with my code?  I keep getting an error message that says my constructor is undefined.  How is my syntax wrong?  Thanks!


Code:
Tank myTank;

void setup() {
 size(400,400);
 smooth();
 myTank = new Tank();
}

void draw() {
 background(0);
 myTank.drawMe();
 myTank.control();
}

class Tank {
 float x, y;
 float theta;

 Tank(float ix, float iy, float itheta) {
   x = ix;
   y = iy;
   theta = itheta;
 }

 void drawMe() {
   strokeWeight(1);
   fill(159,219,169);
   ellipse(x,y,30,30);
   fill(205,247,207);
   ellipse(x,y,12,12);
   translate(x,y);
   rotate(theta);
   strokeWeight(4);
   line(0,0,25,0);

 }

 void control() {
   theta = atan2(mouseY - y, mouseX - x);
   if (key == 'w' && keyPressed){
     y-=2;
   }
   else if (key == 's' && keyPressed) {
     y+=2;
   }
   else if (key == 'a' && keyPressed) {
     x-=2;
   }
   else if (key == 'd' && keyPressed) {
     x+=2;
   }
 }
}


Re: Class help? Constructor undefined
Reply #1 - Nov 14th, 2009, 11:32am
 
The error may seem slightly misleading, but it actually makes sense.  Because Processing allows you to create overloaded constructors you can have a constructor that takes no arguments: "Tank()" as well as a constructor that does take arguments.  The only constructor you have defined takes 3 arguments: "Tank(float ix, float iy, float itheta)", but you haven't passed those arguments when you call the constructor...
Page Index Toggle Pages: 1