I write this code which has two car that should moving forward . but unfortunately just one of these cars work ! the other one is not even in the screen . how could I fix this ?
one other question is that I have some text files for the positions of these cars , right now each car has one text file , how could I mix that and use just one text file for all of that car objects?
String[] lines;
int index = 0;
Car myCar1;
Car myCar2;
void setup() {
size(800, 800);
background(255);
myCar1 = new Car(color(255)); //first car
myCar2 = new Car(color(150)); //second car
}
void draw() {
lines = loadStrings("positions.txt"); //loading the positions from text file and draw the car object1
myCar1.Move();
lines = loadStrings("positionss.txt"); loading the positions from text file and draw the car object2
myCar2.Move();
}
class Car {
color c;
int z = 5;
int t = 15;
Car(color tempC){
c = tempC;
}
void Move() {
fill(c);
if (index < lines.length) {
background(255);
String[] pieces = split(lines[index], '\t');
if (pieces.length == 2) {
int x = int(pieces[0]);
int y = int(pieces[1]);
rect(x, y, z, t);
}
// Go to the next line for the next run through draw()
index = index + 1;
}
}
}
1