We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How do i put this in an array
Car myCar1;
Car myCar2; // Two objects!
Car myCar3;
Car myCar4;
Car myCar5;
Car myCar6;
Car myCar7;
Car myCar8;
Car myCar9;
Car myCar10;
void setup() {
size(250,250);
myCar1 = new Car(color(255,0,0),0,100,2); // Parameters go inside the parentheses when the object is constructed.
myCar2 = new Car(color(0,0,255),0,10,1);
myCar3 = new Car(color(25,247,67),0,112,1);
myCar4 = new Car(color(25,247,231),0,40,5);
myCar5 = new Car(color(232,247,25),0,65,7);
myCar6 = new Car(color(232,37,206),0,24,3);
myCar7 = new Car(color(129,109,73),0,150,2);
myCar8 = new Car(color(250,96,0),0,175,4);
myCar9 = new Car(color(26,92,232),0,200,2);
myCar10 = new Car(color(26,232,147),0,240,6);
}
void draw() {
background(255);
myCar1.move();
myCar1.display();
myCar2.move();
myCar2.display();
myCar3.move();
myCar3.display();
myCar4.move();
myCar4.display();
myCar5.move();
myCar5.display();
myCar6.move();
myCar6.display();
myCar7.move();
myCar7.display();
myCar8.move();
myCar8.display();
myCar9.move();
myCar9.display();
myCar10.move();
myCar10.display();
}
class Car { // 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.Isn’t object-oriented programming swell?
color c;
float xpos;
float ypos;
float xspeed;
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { // The Constructor is defined with arguments.
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}
void move() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
}
Answers
1st, when posting a code here, highlight it and then hit CTRL+K to format it! :-w
And the article link below is gonna surely help ya out: :-B
http://wiki.processing.org/w/From_several_variables_to_arrays
I am still unsure about what to do
instead of Car car1... Car car10 you can write
Car[] car = new Car[10];
then reference them as car[0], car[1], ... car[9]
which doesn't seem much, until you realise that you can use a variable as the index and a for-loop rather than calling them all individually.
NB array indexes start at 0, not one, hence 0-9 rather than 1-10
this is a better link that the one GotoLoop posted http://www.homeandlearn.co.uk/java/java_arrays.html (and the following page)
similar to this?
yeah, but remember what i said about for loops? lines 18-37 are crying out for a for loop...
see, when i see something that looks like a homework question, i try to answer in hints rather than just pasting code...
I am not sure about the error it is giving me.
Posting unformatted code this way makes help more difficult! :((
Have you read what I said above on how to format posted code? X(
"I am not sure about the error it is giving me."
Would have helped to give the error message and the line where you get it...
Fortunately, we can run this code...
makes no sense!
Should be:
ie. you must use the array you created.
Fixed your code: =:)