Not working arrays
in
Programming Questions
•
2 years ago
I am unsure why when it runs the first time all the rectangles are on top of each other and then they separate the second time through. Also why are my vertical rectangles not move?!
I also want all of the rectangles to stay on the screen instead of all disappearing when the timer starts over, I am new at this and don't know how to have that happen.
Thanks in advance
- Timer_Car timer_Car;
- Car[] horizontalCar;
- Car[] verticalCar;
- int totalHorizontalCar = 0;
- int totalVerticalCar = 0;
- void setup() {
- size(600, 600);
- smooth();
- timer_Car = new Timer_Car(6000);
- timer_Car.start();
- horizontalCar = new Car[50];
- verticalCar = new Car [20];
- for(int i = 0; i < horizontalCar.length; i ++){
- horizontalCar[i] = new Car(color(random(255), random(255), random(255)), 0, i*2, i/20);
- }
- for(int j = 0; j < verticalCar.length; j ++){
- verticalCar[j] = new Car(color(random(255), random(255), random(255)),j*2, 0, j/20);
- }
- }
- void draw() {
- background(255);
- if(timer_Car.isFinished()){
- for(int i = 0; i < horizontalCar.length; i++){
- horizontalCar[totalHorizontalCar] = new Car(color(random(255), random(255), random(255)), -10, random(0, height), random(1, 3));
- totalHorizontalCar ++;
- }
- for(int j = 0; j < verticalCar.length; j ++){
- verticalCar[totalVerticalCar] = new Car(color(random(255), random(255), random(255)),random(0, width), 0, random(1, 3));
- totalVerticalCar ++;
- }
- if(totalHorizontalCar >= horizontalCar.length){
- totalHorizontalCar = 0;
- }
- if(totalVerticalCar >= verticalCar.length){
- totalVerticalCar = 0;
- }
- timer_Car.start();
- }
- for (int i = 0; i < horizontalCar.length; i ++ ) {
- horizontalCar[i].moveHorizontal();
- horizontalCar[i].display();
- }
- for (int j = 0; j < verticalCar.length; j ++ ) {
- verticalCar[j].moveVertical();
- verticalCar[j].display();
- }
- }
- class Car {
- color c;
- float xpos;
- float ypos;
- float xspeed;
- float yspeed;
- Car(color c_, float xpos_, float ypos_, float xspeed_) {
- c = c_;
- xpos = xpos_;
- ypos = ypos_;
- xspeed = xspeed_;
- }
- void display() {
- rectMode(CENTER);
- stroke(0);
- fill(c);
- rect(xpos,ypos,20,10);
- }
- void moveHorizontal() {
- xpos = xpos + xspeed;
- if (xpos > width) {
- xpos = 0;
- }
- }
- void moveVertical() {
- ypos = ypos + yspeed;
- if (ypos > height) {
- ypos = 0;
- }
- }
- class Timer_Car {
- int savedTime; // When Timer started
- int totalTime; // How long Timer should last
- Timer_Car(int tempTotalTime) {
- totalTime = tempTotalTime;
- }
- // Starting the timer
- void start() {
- // When the timer starts it stores the current time in milliseconds.
- savedTime = millis();
- }
- // The function isFinished() returns true if "totalTime" has passed.
- // The work of the timer is farmed out to this method.
- boolean isFinished() {
- // Check how much time has passed
- int passedTime = millis()- savedTime;
- if (passedTime > totalTime) {
- return true;
- } else {
- return false;
- }
- }
- }
1
