-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