Objects tutorial by Daniel Shiffman - code doesn't run

Hello, I'm going through the tutorials, and obviously the one about Objects is quite confusing since it introduces a lot of new things...

In any case, I tried to run the following code:

class Car {
  color c;
  float xpos;
  float ypos;
  float xspeed;

Car(color tempC, float tempXpos, float tempYpos, float tempSpeed) {
  c = tempC;
  xpos = tempXpos;
  ypos = tempYpos;
  xspeed = tempXspeed;
  }
}

Car myCar1;
Car myCar2;

void setup()  {
size(500,500);
myCar1 = new Car(color(255,0,0),0,200,2);
myCar2 = new Car(color(0,0,255),0,100,1);
}

void draw()  {
background(0);
myCar1.drive();
myCar1.display();  
myCar2.drive();
myCar2.display();  
}

void display()  {
  stroke(255);
  fill(c);
  rectMode(CENTER);
  rect(xpos,ypos,50,30);
} 

void drive()  {
  xpos = xpos + xspeed;
  if (xpos > width) {
    xpos = 0;
  }
}

The error I get is - Cannot find anything named "tempXspeed"

Can anyone help me understand exactly the "order" let's say, of how the code "goes"? I don't know if to put the class at the start or before/after specific things, because the tutorial doesn't say... I mean, it isn't clear if the position of sections of the code is important...

In addition of course, the error above refers to the tempXspeed variable, but again, i'm not sure why this is or how to solve it.

Thank you.

Answers

  • In line 7 change tempSpeed to tempXspeed

    Lines 1 - 13 represents the blueprint for creating Car(s)

    Lines 15 & 16 you are 'declaring' two object references of type Car.

    lines 20 & 21 are creating (aka 'instantiating') two car objects using the new keyword and a 'constructor'

    lines 7-12 is a constructor, you can tell because it has exactly the same name as the class and does not have a return data type,, not even void

    The class code can be anywhere in your sketch provided you keep it together. Generally I suggest you put it at the bottom or a separate pde tab because once written you rarely (at least as not as often as the rest of the code e.f. draw method) need to view the class code.

    HTH

  • Cannot find anything named "tempXspeed"

    @ xspeed = tempXspeed;, it's trying to assign value of tempXspeed to field xspeed.
    According to Car(color tempC, float tempXpos, float tempYpos, float tempSpeed), parameter is called tempSpeed, not tempXspeed! It's true, parameters are local variables too! ~O)

  • It also tells me that "The function drive() does not exist". This error highlights line 26.

    I think maybe I should call "myCar1.display" ?? or maybe something about the brackets is misplaced...

    Any idea? Thanks.

  • "The function drive() does not exist".

    Your class Car is incomplete. There's only its constructor there. It got no methods yet! [-X

  • You should try moving the drive and display functions you do have to inside your Car class (that is, inside the {} pair that starts on the class Car{ line!).

  • class Car {
      color c;
      float xpos;
      float ypos;
      float xspeed;
    
    myCar1.drive();
    myCar1.display();  
    myCar2.drive();
    myCar2.display();
    
    Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
      c = tempC;
      xpos = tempXpos;
      ypos = tempYpos;
      xspeed = tempXspeed;
      }
    }
    
    Car myCar1;
    Car myCar2;
    
    void setup()  {
    size(500,500);
    myCar1 = new Car(color(255,0,0),0,200,2);
    myCar2 = new Car(color(0,0,255),0,100,1);
    }
    
    void draw()  {
    background(0);
    
    
    void display()  {
      stroke(255);
      fill(c);
      rectMode(CENTER);
      rect(xpos,ypos,50,30);
    } 
    
    void drive()  {
      xpos = xpos + xspeed;
      if (xpos > width) {
        xpos = 0;
      }
    }
    

    Should I change lines 7-10 to "void drive()" etc? I'm totally confused...

  • Not the drive() and display() calls, the functions, the definitions themselves. The calls must remain in draw().

  • Answer ✓

    here

    // class ===============================
    
    class Car {
      color c;
      float xpos;
      float ypos;
      float xspeed;
    
      Car(color tempC, 
      float tempXpos, float tempYpos, 
      float tempXspeed) {
        c = tempC;
        xpos = tempXpos;
        ypos = tempYpos;
        xspeed = tempXspeed;
      } // constructor
    
      void display() {
        stroke(255);
        fill(c);
        rectMode(CENTER);
        rect(xpos, ypos, 50, 30);
      } // method 
    
      void drive() {
        xpos = xpos + xspeed;
        if (xpos > width) {
          xpos = 0;
        } // if
      } // method
    } // class 
    
    // end of class ==============================
    
    Car myCar1;
    Car myCar2;
    
    void setup() {
      size(500, 500);
      // we invoke 2 different cars  
      myCar1 = new Car(color(255, 0, 0), 0, 200, 2);
      myCar2 = new Car(color(0, 0, 255), 0, 100, 1);
    }
    
    void draw() {
      background(0);
    
      // we use the methods inside the class 
      myCar1.drive();
      myCar1.display();  
      myCar2.drive();
      myCar2.display();
    }
    
    // 
    
  • edited December 2014 Answer ✓

    the thing about classes or OOP is that with a class you can have an item like a car in your code in one place. Very tidy.

    otherwise (without the OOP architecture) you would have variables for the car and functions responsible for these car variables just somewhere in your code:

      color CarC;
      float Carxpos;
      float Carypos;
      float Carxspeed;
    

    with OOP you can have the properties and the functions responsible for these in a neat package called class Car. Very good. See above.

    when invoking a 2nd car you use the same class but with different values for the properties. So the car is the cookie but the class is the cookie maker. When we invoke a car from the class we say we instantiate one object (real item) from the class (the abstract idea of the item).

    OK?

    ;-)

Sign In or Register to comment.