-1
我在Processing(JavaScript)中创建了一个游戏,它允许您在逐个突出显示矩形的网格时为它着色。我想使用for循环和fill()函数为一个对象数组着色,但是当我使用它时,它的颜色不仅给数组中的当前矩形着色(这将是正确的),但是,当我使用它时,它不仅着色数组中的当前矩形也是以前的所有矩形。我希望它只为当前的矩形着色。使用循环的对象使用fill()
void setup() {
size (1200, 700);
background (255, 255, 255);
smooth();
rect = new Rectangle [count]; //creates a grid of possible rectangles, based on the dimensions of the screen
for (int i = 0; i<= (count-1); i++) {
fill (255, 255, 255);
ypos = ypos + heigh;
if (ypos >= 700) {
ypos = 0;
xpos = xpos + wid;
}
rect[i] = new Rectangle(xpos, ypos, wid, heigh, redPressed, greenPressed, yellowPressed, bluePressed, blackPressed);
}
int now = millis();
}
void draw() {
for (int i = 0; i < control; i++) {
if (keyPressed) { //detects if key is pressed and colors the current rectangle that way
if (key == 'r' ||key == 'R') {
fill (255, 0, 0);
}
if (key == 'g' ||key == 'G') {
fill (0, 255, 0);
}
if (key == 'y' || key == 'Y') {
fill (255, 255, 0);
}
if (key == 'b' || key == 'B') {
fill (0, 0, 255);
}
if (key == 'k' || key == 'K') {
fill (0, 0, 0);
}
}
stroke (0, 0, 0);
rect (rect[i].xpos, rect[i].ypos, rect[i].xdim, rect[i].ydim); //draws the current rectangle, moving through the grid
}
if (millis() - now >= wait) { //causes a 1 second delay between rectangles
now = millis();
control++;
if (control > (count-1)) {
control = (count-1);
i = 0;
}
}
}
在此先感谢!
for循环中的'control'变量是什么?它看起来像绘图函数可能会绘制多个矩形,因为它在for循环中。另外,你如何确定数组中的“当前矩形”? – miir
@Bergi控制变量在每个矩形的绘制之间强制延迟1秒。它每1秒增加1。而通过“当前矩形”,我的意思是无论什么价值“我”是在for循环。所以当我= 1时,当前的三角形是rect [1]。 – lgouvin
您的意思是ping @AmirS – Bergi