Hi all,
I’m trying to translate code from Processing to p5. The code embeds one class (Wheel) inside another (Car). I think that I’m close, but I’m getting an error message on line 70: backWheel is not defined.
Is my issue line 51 and 52?:
let backWheel;
let frontWheel;
Should I be initializing these variables somewhere else, and if so, where?
I’m using OpenProcessing.org as an editor if that’s helpful.
Here’s the full code:
let dealership = new Array(15);
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB);
angleMode(DEGREES);
dealership[0] = new Car(50, 80 + 0 * 60, 0 * 10, random(1, 2));
}
function draw() {
background(50);
dealership[0].displayCar();
dealership[0].moveCar();
}
class Wheel {
// --------- Wheel: Attributes ---------//
constructor(tempX1, tempY1, tempAngle1, tempColor1) {
this.x1 = tempX1;
this.y1 = tempY1;
this.angle1 = tempAngle1;
this.color1 = tempColor1;
}
//--------- Wheel: Display ---------//
displayWheel() {
fill(this.color1, 255, 255);
push();
translate(this.x1 - 25, this.y1);
rotate(this.angle1);
ellipse(0, 0, 15, 20);
pop();
}
//-------- Wheel: Turn ---------//
turnWheel() {
this.angle1 = this.angle1 + 1; // The 1 rotates the ellipse by 1 degree
}
}
class Car {
// --------- CAR: Attributes ---------//
constructor(tempX, tempY, tempPaint, tempSpeed) {
this.x = tempX;
this.y = tempY;
this.paint = tempPaint;
this.speed = tempSpeed;
let backWheel; //-------------------------------------------------------------------- WHEEL
let frontWheel; //-------------------------------------------------------------------- WHEEL
backWheel = new Wheel(this.x - 25, this.y, 0, 52); //-------------------------------------------------------------------- WHEEL
frontWheel = new Wheel(this.x + 25, this.y, 0, 91); //-------------------------------------------------------------------- WHEEL
}
//--------- CAR: Display ---------//
displayCar() {
rectMode(CENTER);
fill(this.paint, 255, 255);
rect(this.x - 10, this.y - 30, 50, 20);
rect(this.x, this.y - 10, 75, 20);
fill(212, 242, 252);
rect(this.x + 5, this.y - 30, 15, 15);
// fill(0);
// ellipse(this.x - 25, this.y, 20, 20);
// ellipse(this.x + 25, this.y, 20, 20);
backWheel.displayWheel(); //-------------------------------------------------------------------- WHEEL
frontWheel.displayWheel(); //-------------------------------------------------------------------- WHEEL
}
//-------- CAR: Move ---------//
moveCar() {
this.x += this.speed;
backWheel.turnWheel(); //-------------------------------------------------------------------- WHEEL
frontWheel.turnWheel(); //-------------------------------------------------------------------- WHEEL
}
}