-1

I am trying to get this bit of code I have got and add another traffic light and car but going from right to left not from bottom to top. The car only moves when the light is amber or green and stops when red. I have tried copying the code and changing the names of the values but it just will not work. I have tried naming them all different names but it won't even show the other image I tried to put in for the other car. Can someone please tell/show me how I could add another traffic light and car but they move at different times?

TrafficLight light1 = new TrafficLight(100, 40);
int onTime = 2000;
int startTime = millis();
PImage car;
int carX, carY;

void setup() {
    size(800, 600);
    light1.changeColour("red");
    light1.display();

    car = loadImage("Car.png");
    carX = 150;
    carY = 300;
}

void draw() {
    background(255);


    if (millis() - startTime > onTime && light1.lightOn == "red") {
        light1.changeColour("amber");
        startTime = millis();
    }
    if (millis() - startTime > onTime && light1.lightOn == "amber") {
        light1.changeColour("green");
        startTime = millis();
    }
    if (millis() - startTime > onTime && light1.lightOn == "green") {
        light1.changeColour("red");
        startTime = millis();
    }


    light1.display();
    image(car, carX, carY);
    if (light1.lightOn == "green") {
        carY -= 2;
    }
    {
    }
    if (light1.lightOn == "red") {
        carY -= 0;
    }
    {
    }
    if (light1.lightOn == "amber") {
        carY -= 1;
    }
    {
    }

    if (carY <= -200) {
        carY = 500;
    }
}


class TrafficLight {
    int xpos;
    int ypos;
    String lightOn = "red";

    TrafficLight(int x, int y) {
        xpos = x;
        ypos = y;
    }

    void changeColour(String lightColour) {
        lightOn = lightColour;
    }

    void display() {
        String lightColour = lightOn;
        fill(0, 0, 0);
        rect(xpos, ypos, 100, 220);//back panel
        if (lightColour == "red") {
            fill(255, 0, 0);
            lightOn = "red";
        } else {
            fill(100, 0, 0);
        }
        ellipse(xpos + 50, ypos + 40, 60, 60);//red
        if (lightColour == "amber") {
            fill(255, 255, 0);
            lightOn = "amber";
        } else {
            fill(100, 100, 0);
        }
        ellipse(xpos + 50, ypos + 110, 60, 60);//amber  
        if (lightColour == "green") {
            fill(0, 255, 0);
            lightOn = "green";
        } else {
            fill(0, 100, 0);
        }
        ellipse(xpos + 50, ypos + 180, 60, 60);//green
    }

}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Vincent
  • 1
  • 1
  • Two things: you need to describe your actual problem (as in what's going wrong), and `light1.lightOn == "red"` [ain't gonna work](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Makoto May 09 '16 at 22:42
  • Please ask a specific question. Something like *I tried X to achieve Y using library Z but got exception E with stacktrace S* is appropriate for StackOverflow. – Debosmit Ray May 09 '16 at 22:45

1 Answers1

0

The code you've posted only contains one car, which makes this question hard to answer. Stack Overflow isn't really designed for general "how do I do this" type questions. It's more designed for specific "I tried X, expected Y, but got Z instead" type questions. That being said, I'll try to answer in a general sense:

Here's the short answer: you need to create a state for a second car, and then work with that state exactly how you work with the state of the first car. This might be easier if you encapsulate that state into a class, and then use two instances of that class.

Let's start out with a simpler example of a circle that moves vertically, and stops when we press the mouse:

MovingCircle verticalCircle;

void setup() {
  size(600, 600);
  verticalCircle = new MovingCircle(250, 250, 0, 5);
}

void mousePressed() {
  verticalCircle.moving = !verticalCircle.moving;
}

void draw() {
  background(0);
  verticalCircle.step();
}

class MovingCircle {

  float x;
  float y;
  float xSpeed;
  float ySpeed;
  boolean moving = true;

  public MovingCircle(float x, float y, float xSpeed, float ySpeed) {
    this.x = x;
    this.y = y;
    this.xSpeed = xSpeed;
    this.ySpeed = ySpeed;
  }

  void step() {

    if (moving) {
      x+=xSpeed;
      y+=ySpeed;

      if (x > width) {
        x = 0;
      }
      if (y > height) {
        y = 0;
      }
    }

    ellipse(x, y, 25, 25);
  }
}

This program creates a circle that moves down the screen, and stops and starts when you click the mouse. Notice that I've encapsulated all of the information I need inside the MovingCircle class, so now if I want to add another one, I can just create another instance of that class and interact with it however I want.

Here's how I would add a horizontally moving circle:

MovingCircle verticalCircle;
MovingCircle horizontalCircle;

void setup() {
  size(600, 600);
  verticalCircle = new MovingCircle(250, 250, 0, 5);
  horizontalCircle = new MovingCircle(250, 250, 5, 0);
}

void mousePressed() {
  verticalCircle.moving = !verticalCircle.moving;
}

void keyPressed() {
  horizontalCircle.moving = !horizontalCircle.moving;
}

void draw() {
  background(0);
  verticalCircle.step();
  horizontalCircle.step();
}

This is just an example, but the principle is the same in your code: you need to encapsulate your state into a class, and then you can work with two instances of that class in order to have two moving cars.

Also, you should never use == to compare String values! Use the .equals() function instead!

if(lightColour.equals("red")){
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107