我正在用Arduino和Processing创建一种游戏。在我的代码中,我使用了Daniel Shiffman的类Timer,但是想使用该类的两个不同实例创建两个不同的Timers。
我的问题是这两个实例似乎混淆了,每个实例都在做另一个应该做的事情。
例如,计时器应运行10秒,correctTimer应运行3秒,但它们都运行10秒。此外,当计时器完成时,它应将背景设置为红色,当correctTimer完成时,它应将背景设置为蓝色。但是,两个计时器在完成后将背景设置为蓝色。
有没有人对如何解决这个问题有任何想法?
import processing.serial.*;
int end = 10;
String serial;
Serial port;
float[] array;
// --------------------------------------------------
PImage img;
PImage correct;
PImage incorrect;
float thumb;
float index;
float middle;
float ring;
float pinky;
// --------------------------------------------------
String alphabet;
int randomNum;
String letter;
// --------------------------------------------------
int savedTime;
int totalTime;
int passedTime;
boolean quit = false;
class Timer {
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void start() {
savedTime = millis();
//quit = false;
}
boolean isFinished() {
passedTime = millis() - savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
Timer timer;
Timer correctTimer;
// --------------------------------------------------
boolean checkLetter(String letterPicked, float flexR_THUMB, float flexR_INDEX, float flexR_MIDDLE, float flexR_RING, float flexR_PINKY) {
if (letterPicked == "A") {
if (flexR_THUMB > 12000 && flexR_THUMB < 22000 &&
flexR_INDEX > 27958 && flexR_INDEX < 38500 &&
flexR_MIDDLE > 26035 && flexR_MIDDLE < 41650 &&
flexR_RING > 16492 && flexR_RING < 26000 &&
flexR_PINKY > 37528 && flexR_PINKY < 53500) {
return true;
} else {
return false;
}
}
return false; }
// --------------------------------------------------
void setup() {
size(1280, 950);
background(255);
port = new Serial(this, "/dev/tty.usbmodem1421", 9600);
port.clear();
serial = port.readStringUntil(end);
serial = null;
correct = loadImage("img/RIGHT.png");
incorrect = loadImage("img/WRONG.png");
correctTimer = new Timer(3000);
startOver();
}
// --------------------------------------------------
void startOver() {
background(255);
letter = "A";
img = loadImage("img/" + letter +".png");
image(img, 0, 0, 1280, 950);
timer = new Timer(10000);
timer.start();
}
// --------------------------------------------------
void draw() {
while(port.available() > 0) {
serial = port.readStringUntil(end);
}
if (serial != null) {
float[] array = float(split(serial, ','));
thumb = array[0];
index = array[1];
middle = array[2];
ring = array[3];
pinky = array[4];
}
if (checkLetter(letter, thumb, index, middle, ring, pinky) == true && quit == false) {
image(correct, 0, 0, 1280, 950);
quit = true;
correctTimer.start();
} else if (timer.isFinished() && quit == false) {
background(255, 0, 0);
quit = true;
correctTimer.start();
}
if (correctTimer.isFinished()) {
background(0, 0, 255);
}
}