Motorbike contructor help

im trying to add a constructor to my code and I think I don’t understand how to do it

my code:

Motorbike redBike;
Motorbike blueBike;

void setup()  //called only once at the start
{
  size(500,100);
  Motorbike(30, RED );
  //redBike.y = 30;
  //redBike.colour = RED;
  Motorbike(60, BLUE);
 // blueBike.y = 60;
  //blueBike.colour = BLUE;
}


void draw()  //60 times a second
{
  background(125);
  redBike.update();
  blueBike.update();
  
} 

class code:

final color RED = color(255,0,0);
final color BLUE = color(0,0,255);

class Motorbike
{
 int x = 5;  //members
 int y;
 int speed=2;
 int size=30;
 color colour;

 void render()
 {
   float wheelHeight = size/3;
   fill(colour);
   triangle(x,y,x+size,y,x+size/2,y-size/2);  //built-in triangle routine
   drawWheel(x,y,wheelHeight);
   drawWheel(x+size,y,wheelHeight);
 }
 
 void drawWheel(int x,int y,float size)
 {
    float inner = size*2/3;
    fill(0);
    ellipse(x,y,size,size);
    fill(255);
    ellipse(x,y,inner,inner);
 }
 void move() {
    speed = (int)random(5.0);  //a random step [0..5]
    x=x+speed; 
 }

 void update()  {
   move();
   render();
 }
 
 Motorbike(int y,color col){ //constructor
   this.y = y;
   this.speed = (int)random(5.0);
   this.colour = col;
 }


} //end of class description

so this is my constructor code which I have added to my class Motorbike:

Motorbike(int y,color col){ //constructor
   this.y = y;
   this.speed = (int)random(5.0);
   this.colour = col;
 }

this is what I don’t understand what to do here with that constructor code:

void setup()  //called only once at the start
{
  size(500,100);
  Motorbike(30, RED );
  //redBike.y = 30;
  //redBike.colour = RED;
  Motorbike(60, BLUE);
 // blueBike.y = 60;
  //blueBike.colour = BLUE;
}

Also explain what the constructor does in the code please, that would be helpful to me, thanks.

So i worked it out myself, lol

Motorbike redBike;
Motorbike blueBike;

void setup()  //called only once at the start
{
  size(500,100);
  redBike = new Motorbike(30,RED);
 // redBike.y = 30;
 // redBike.colour = RED;
  blueBike = new Motorbike(60,BLUE);
  //blueBike.y = 60;
  //blueBike.colour = BLUE;
}

1 Like