【发布时间】:2014-06-02 12:16:50
【问题描述】:
我正在尝试制作一个交通模拟器,但我完全低估了让所有课程协同工作的难度。我终于让所有功能都能很好地工作,现在我正在尝试将它们组合在一起。在我的灯光算法中,我已经让灯光在计时器上改变得很好,但是我需要绑定一个附加到灯光颜色的布尔值,然后在该类之外使用它来告诉汽车是否移动(失败惨)。因此,现在我将 Car 类 .move/.draw 引入了灯光类,尽管由于某种原因,布尔值与它们的开关不对应,尽管有一个变量可以改变灯光,所以我不明白为什么当该开关中的灯保持不变时,我的布尔值不断从真切换到假我已经注释掉了我尝试放置 car.draw 部分的几个不同的地方。
我已将代码编辑为类似于布尔标志与灯光变化不对应的方式。
另外,如果我可以让标志起作用,我如何在另一个班级中调用该标志?
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;
}
}
【问题讨论】:
-
确定您的问题。去掉所有不相关的代码。调试是你的朋友。
-
首先我建议您初始化所有布尔标志。现在你只有
Yy到false其他人是null! -
我编辑了上面显示的代码来集中我的问题,有什么帮助吗?请打我的头。
-
不要删除您的问题,发布解决方案的答案并接受它。
-
@MarcoAcierno 抱歉,stackexchange 的新手。
标签: java boolean processing flags