1 回答
TA贡献1712条经验 获得超3个赞
boolean light = false;
您需要一个类的成员变量,而不是单个变量Box
:
class Box {
boolean light = false;
float x, y;
color c;
int size = 50;
Box (int valX, int valY) {
x = valX * size;
y = valY * size;
}
.....
}
现在每个盒子对象都可以保存信息,无论它是否“点亮”。
在draw函数中,您必须绘制所有框,每个框都依赖于其状态,为此
boxes[i][j].light使用 2 个嵌套for循环:
void draw() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
if (boxes[i][j].light == true) {
boxes[i][j].rollover(mouseX, mouseY);
boxes[i][j].displayOn();
} else {
boxes[i][j].displayOff();
}
}
}
}
在mousePressed你必须检查所有Box带有 state 的对象boxes[i][j].light,是否mouseX和mouseY是:
void mousePressed() {
boolean hit = false;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
if (boxes[i][j].light == true && boxes[i][j].onPress(mouseX, mouseY)) {
boxes[i][j].light = false;
hit = true;
}
}
}
println(hit ? "yes" : "no");
}
最后,您必须boxes[randI][randJ].light在函数中设置成员keyPressed,而不是变量light,后者不再存在:
void keyPressed() {
if (boxes[randI][randJ].keyRight()) {
boxes[randI][randJ].light = true;
randI = (int)random(0, cols);
randJ = (int)random(0, rows);
keyIndex = int(random(97, 120));
println(keyIndex, char(keyIndex));
}
}
添加回答
举报