我试图运行此循环两次然后将状态更改为1.(state = 1;)。那可能吗?
public void mousePressed() {
if (state == 0) {
for (int i = 0; i < 6; i++) {
if (mouseX >= cards[i].x && mouseX <= cards[i].x + cards[i].WIDTH && mouseY >= cards[i].y && mouseY <= cards[i].y + cards[i].HEIGHT) {
cards[i].flip();
}
}
state = 1;
}
现在,当我运行此循环时,它只运行一次并将状态形式0(状态== 0)更改为1.我的问题是我可以使此循环运行两次(按下鼠标2次)然后将状态从0更改为1(状态= 1;)。
非常感谢你提前。任何帮助,将不胜感激。
答案 0 :(得分:2)
我认为你想在第二次执行mousePressed
时改为状态1。你需要分裂你的状态。一种方法是引入一个新的状态变量(让我们称之为first
),用于在第一次鼠标点击之前和之后区分state == 0
:
private boolean first = true;
public void mousePressed() {
if (state == 0) {
for (int i = 0; i < 6; i++) {
if (mouseX >= cards[i].x && mouseX <= cards[i].x + cards[i].WIDTH && mouseY >= cards[i].y && mouseY <= cards[i].y + cards[i].HEIGHT) {
cards[i].flip();
}
}
if (first) {
first = false;;
} else {
state = 1;
// perhaps reset first?
}
}
}
第二种方法是允许state
采用与逻辑中其他任何位置处理状态0相同的第三个值。 (从概念上讲,你将拥有状态0和0'。)
答案 1 :(得分:0)
不是100%肯定你在问什么,是这样的:
public void mousePressed() {
state = 0; /* Assuming it is global */
while (state < 2) {
for (int i = 0; i < 6; i++) {
if (mouseX >= cards[i].x && mouseX <= cards[i].x + cards[i].WIDTH && mouseY >= cards[i].y && mouseY <= cards[i].y + cards[i].HEIGHT) {
cards[i].flip();
}
state++;
}
}
每次调用mousePressed
时,这将循环2次。按if
while