2015-10-13 78 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。