Having looked at it again my assumptions were clearly incorrect - I should have paid more attention to the problems with the originally posted code.
I'd noticed that the OP hadn't invoked the 'direction' method in the draw loop, but hadn't noticed that the keyPressed condition checked against CAPITALS... [slaps forehead]
The boolean approach is only really relevant if you want motion only for as long as the key is pressed.
Code:Car myCar;
void setup() {
size(200,200);
// Parameters go inside the parentheses when the object is constructed.
myCar = new Car(color(255,0,0),0,100,0,0);
}
void draw() {
background(255);
myCar.move();
myCar.display();
myCar.direction();
}
// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
float xspeed;
float yspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
yspeed = tempYspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}
void move() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
if (xpos < 0) {
xpos = width;
}
ypos = ypos + yspeed;
if (ypos > height) {
ypos = 0;
}
if (ypos < 0) {
ypos = height;
}
}
void direction() {
if (keyPressed) {
if (key == 'a') {
xspeed = -1;
}
if (key == 'd') {
xspeed = 1;
}
if (key == 'w') {
yspeed = 1;
}
if (key == 's') {
yspeed = -1;
}
}
}
}