2015-10-13 23 views
-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; 
    } 
    } 
} 

在此先感謝!

+0

for循環中的'control'變量是什麼?它看起來像繪圖函數可能會繪製多個矩形,因爲它在for循環中。另外,你如何確定數組中的「當前矩形」? – miir

+0

@Bergi控制變量在每個矩形的繪製之間強制延遲1秒。它每1秒增加1。而通過「當前矩形」,我的意思是無論什麼價值「我」是在for循環。所以當我= 1時,當前的三角形是rect [1]。 – lgouvin

+0

您的意思是ping @AmirS – Bergi

回答

1

首先,因爲你還沒有發佈您的Rectangle類或者control, '計數', 'XPOS', 'ypos',widheighredPressedgreenPressedyellowPressed我們無法運行此代碼, bluePressedblackPressed變量。變量。如果您需要幫助,您必須發佈我們可以實際運行的MCVE

其次,你在這裏做了一些奇怪的事情:爲什麼你循環你的Rectangle陣列,如果你只想一次畫出其中的一個?爲什麼你手動導致等待而不是設置幀率?

不是每次調用draw()時都會遍歷矩形,請跟蹤索引並直接訪問它。不要自己動手,只需設置幀率:

int index = 0; 
Rectange[] rect; 

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(); 
    frameRate(1); 
} 

void draw() { 

    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[index].xpos, rect[index].ypos, rect[index].xdim, rect[index].ydim); 
    index++; 
    if (index >= rect.length) { 
    noLoop(); 
    } 
} 

請下次嘗試發佈一個MCVE。