简体   繁体   English

处理-布尔标志与它们所在的开关不对应

[英]Processing - Boolean flags aren't corresponding to the switch they are in

Trying to make a traffic simulator and completely underestimated how hard it was going to be to get all the Classes to work together. 试图制作一个交通模拟器,并完全低估了使所有类一起工作的艰辛。 Anyways I've finally gotten all the functions to work quite well and am now trying to put them together. 无论如何,我终于让所有功能都能很好地工作,现在正在尝试将它们组合在一起。 On my lights algorithm I've gotten the lights to change fine on a timer but then I needed to tie in a boolean that was attached to the lights color and then use it outside that class to tell the car to move or not(Failed Miserably). 在我的灯光算法中,我已经使灯光在计时器上变化良好,但是随后我需要绑定一个附加到灯光颜色的布尔值,然后在该类之外使用它来告诉汽车是否移动( )。 So now I've brought the Car classes .move/.draw to the lights class instead, although for some reason the booleans won't correspond to the switch their in although there's a variable that changes the light so I don't see why my boolean continually switches from true to false when the lights in that switch are staying the same I've put in println to show how the lights are changing fine but the flags arent. 因此,现在我将Car类.move / .draw引入了lights类,尽管由于某些原因,布尔型将不对应于它们的开关,尽管有一个变量会更改灯光,所以我不明白为什么当该开关中的灯保持不变时,我的布尔值会从true连续切换为false,这是我在println中放置的,以显示灯如何正常变化,但标志没有变化。 I've commented out a couple different places I've tried placing the car.draw sections. 我已在尝试放置car.draw部分的几个不同地方进行了注释。

EDIT: Ok ive editted the code to just resemble how the boolean flags aren't corresponding with the lights changing. 编辑:好了,我将代码编辑为仅类似于布尔标志与灯光变化不对应的方式。

Also if I can get the flags to work how can I call upon that flag in another class? 另外,如果我可以使这些标志起作用,那么如何在另一个类中调用该标志?

  int greenX, greenY = 50;   // Alphas of Green Lights (50 = OFF & 250 = ON)
  int redX, redY = 50;       // Alphas of Red Lights (50 = OFF & 250 = ON)
  int yellowX, yellowY = 50; // Alphas of Yellow Lights (50 = OFF & 250 = ON) 
  int time;                  // set to equal millis()
  int lastInterval;          // Used to update time since last light change then used to take away from "time" in order to get amount of time specified
  float interval = 5;        // Interval for Lights in Seconds
  float wait = 1000*interval; // turn interval from seconds to millis
  char lightX = 'G';         // Switch for Lights on X-Cords
  char lightY = 'R';         // Switch for Lights on Y-Cords
  boolean X, Y, Xy, Yy;  // Booleans for Lights to corespond to draw/make car (Xy & Yy = yellow lights)

void setup() {
}

  void draw() {
    time = millis();
    println("Last Interval Times is " + lastInterval/1000);

    if (time - lastInterval < wait) { //  // If the Light has been green for less than wait time
      if ((X == true) && (Y == false)) {
    }
      println("X-Light = " + X);
      println("Y-Light = " + Y);
    } 

    if (time - lastInterval >= wait) {  // If the Light has been green for more than wait time
        X = !X;
        Y = !Y;
      if (lightX == 'G' && lightY == 'R') {
        lightX = 'Y';
        lightY = 'R';
      } 
      else if (lightX == 'Y' && lightY == 'R') {
        lightX = 'R';
        lightY = 'G';
      } 
      else if (lightX == 'R' && lightY == 'G') {
        lightX = 'R';
        lightY = 'Y';
      }
      else if (lightX == 'R' && lightY == 'Y') {
        lightX = 'G';
        lightY = 'R';
      }
      lastInterval = time; // Update last Interval to time of light change
    }


    switch(lightX) {        //Switch For X-Cord Lights
    case 'G':              // Green Light
      println("X-Axis = Green Light");
      time = millis();
      greenX = 250;
      yellowX = 50;
      redX = 50;
      X = true;
      Xy = true;
      break;

    case 'Y':              // Yellow Light
      println("X-Axis = Yellow Light");
      time = millis();
      greenX = 50;
      yellowX = 250;
      redX = 50;
      Xy = true;
      X = !X;
      break;


    case 'R':             //Red Light
      println("X-Axis = Red Light");
      time = millis();
      greenX = 50;
      yellowX = 50;
      redX = 250;
      Xy = !X;
      X = !X;
      break;
    }

    switch(lightY) {       //Switch For Y-Cord Lights
    case 'G':              // Green Light
      println("Y-Axis = Green Light");
      println(time/1000);
      println();
      time = millis();
      greenY = 250;
      yellowY = 50;
      redY = 50;
      Y = true;
      Yy = true;
      break;

    case 'Y':              // Yellow Light
      println("Y-Axis = Yellow Light");
      println(time/1000);
      println();
      time = millis();
      greenY = 50;
      yellowY = 250;
      redY = 50;
      Yy = true;
      Y = !Y;
      break;


    case 'R':             //Red Light
      println("Y-Axis = Red Light");
      println(time/1000);
      println();
      time = millis();
      greenY = 50;
      yellowY = 50;
      redY = 250;
      Y = !Y;
      Yy = !Yy;
      break;
    }
  }

I was using !X to represent false. 我使用!X表示假。 SOLVED. 解决了。 Revised class code posted below, for a light switch that includes green, yellow, and red lights. 下面发布的修订的类代码,用于包括绿色,黄色和红色指示灯的电灯开关。

class LightAlg {
  int greenX, greenY = 50;   // Alphas of Green Lights (50 = OFF & 250 = ON)
  int redX, redY = 50;       // Alphas of Red Lights (50 = OFF & 250 = ON)
  int yellowX, yellowY = 50; // Alphas of Yellow Lights (50 = OFF & 250 = ON) 
  int time;                  // set to equal millis()
  int lastInterval;          // Used to update time since last light change then used to take away from "time" in order to get amount of time specified
  float interval = 5;        // Interval for Lights in Seconds
  float wait = 1000;         // turn interval from seconds to millis
  char lightX = 'G';         // Switch for Lights on X-Cords
  char lightY = 'R';         // Switch for Lights on Y-Cords
  boolean X, Y, Xy, Yy;      // Booleans for Lights to corespond to draw/make car (Xy & Yy = whether intersection should be crossed if already entered by cars)

  LightAlg (float interval_) { //Constructor and specify light change time wanted
    interval = interval_;
  }

  void run() {
    time = millis(); //Sets time to time since program was executed
    println("Last Interval Times is " + lastInterval/1000); // Prints time since last time lights where changed
    println("X-Light = " + X); //Prints Boolean of X-Light 
    println("Xy-Light = " + Xy); //Prints Boolean of X-Light when it supposed to be yellow in order to creat a flag for whether cars should continue throught intersection
    println("Y-Light = " + Y); //Prints Boolean of Y-Light
    println("Yy-Light = " + Yy);//Prints Boolean of Y-Light when it supposed to be yellow in order to creat a flag for whether cars should continue throught intersection
    if (time - lastInterval < wait * interval) { //  // If the Light has been green for less than wait time by taking millis() - time since last light change < time wanted for light interval
      if (X == true && Xy == true) { //If light is green and intersection is ok to enter
        my_array.draw_cars();
        my_array.make_car();
        my_array.check_collision();
      }
    } 

    if (time - lastInterval >= wait * interval) {  // If the Light hasn't changed for longer then specified interval time

      if ((X == false && Xy == false) && (Y == false && Yy == true)) { //If X-Light is Red and Intersection is not safe to cross && Y-Light is yellow and Intersections is safe to cross
        lightX = 'G'; //X-Light Goes Green
        lightY = 'R'; //Y-Light Goes Red
      } 
      else if ((X == true && Xy == true) && (Y == false && Yy == false)) { //If X-Light is Green and Intersection is safe to cross && Y-Light is Red and Intersections is  not safe to cross
        lightX = 'Y'; //X-Light Goes Yellow
        lightY = 'R'; //Y-Light Goes Red
      } 
      else if ((X == false && Xy == true) && (Y == false && Yy == false)) { // If X-Light is Yellow and Intersection is safe to cross && Y-Light is Red and intersection is not safe to cross
        lightX = 'R'; //X-Light Goes Red
        lightY = 'G'; //Y-Light Goes Green
      }
      else if ((X == false && Xy == false) && (Y == true && Yy == true)) { // If X-Light is Red and Intersection is not safe to cross && Y-Light is Green and intersection is safe to cross
        lightX = 'R'; //X-Light Goes Red
        lightY = 'Y'; //Y-Light Goes Green
      }
      else if ((X == false && Xy == false) && (Y == false && Yy == true)) { // If X-Light is Red and Intersection is not safe to cross && Y-Light is Yellow and Intersection is safe to cross
        lightX = 'G'; //X-Light Goes Green
        lightY = 'R'; //Y-Light Goes Red
      }
      lastInterval = time; // Update last Interval to time of light change so that you can Minus it from time in order to set time back to length of interval desired
    }


    switch(lightX) {        //Switch For X-Cord Lights
    case 'G':              // Green Light
      println("X-Axis = Green Light");
      time = millis(); //Update time when light changes
      greenX = 250; //Alphas for trafficlight Object
      yellowX = 50; //Alphas for trafficlight Object
      redX = 50; //Alphas for trafficlight Object
      X = true; //Light is Green so boolean for cars on Xpos BEFORE intersection is true
      Xy = true;
      break;

    case 'Y':              // Yellow Light
      println("X-Axis = Yellow Light");
      time = millis(); //Update time when light changes
      greenX = 50; //Alphas for trafficlight Object
      yellowX = 250; //Alphas for trafficlight Object
      redX = 50; //Alphas for trafficlight Object
      Xy = true; //Light is Yellow so boolean for cars on Xpos INSIDE/AFTER intersection is true
      X = false; //Light is Yellow so boolean for cars on Xpos BEFORE intersection is false
      break;


    case 'R':             //Red Light
      println("X-Axis = Red Light");
      time = millis(); //Update time when light changes
      greenX = 50; //Alphas for trafficlight Object
      yellowX = 50; //Alphas for trafficlight Object
      redX = 250; //Alphas for trafficlight Object
      Xy = false; //Light is RED so boolean for cars on Xpos INSIDE/AFTER intersection is false
      X = false; //Light is Red so boolean for cars on Xpos BEFORE intersection is false
      break;
    }

    switch(lightY) {       //Switch For Y-Cord Lights
    case 'G':              // Green Light
      println("Y-Axis = Green Light");
      println(time/1000);
      println();
      time = millis(); //Update time when light changes
      greenY = 250; //Alphas for trafficlight Object
      yellowY = 50; //Alphas for trafficlight Object
      redY = 50; //Alphas for trafficlight Object
      Y = true; //Light is GREEN so boolean for cars on Ypos BEFORE intersection is true
      Yy = true; //Light is GREEN so boolean for cars on Ypos INSIDE/AFTER intersection is true
      break;

    case 'Y':              // Yellow Light
      println("Y-Axis = Yellow Light");
      println(time/1000);
      println();
      time = millis(); //Update time when light changes
      greenY = 50; //Alphas for trafficlight Object
      yellowY = 250; //Alphas for trafficlight Object
      redY = 50; //Alphas for trafficlight Object
      Yy = true; //Light is YELLOW so boolean for cars on Ypos INSIDE/AFTER intersection is true
      Y = false; //Light is YELLOW so boolean for cars on Ypos BEFORE intersection is false
      break;


    case 'R':             //Red Light
      println("Y-Axis = Red Light");
      println(time/1000);
      println();
      time = millis(); //Update time when light changes
      greenY = 50; //Alphas for trafficlight Object
      yellowY = 50; //Alphas for trafficlight Object
      redY = 250; //Alphas for trafficlight Object
      Y = false;  //Light is RED so boolean for cars on Ypos BEFORE intersection is false
      Yy = false; //Light is RED so boolean for cars on Ypos INSIDE/AFTER intersection is false
      break;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM