PShape and setFill(false) buggy?

In the following code I toggle setFill on a PShape with a mouseclick. It works exactly 3 times/clicks on my system and then sticks on the fill color and ignores setFill(false). Am I doing something wrong or is setFill buggy? If I use setFill(0,255,0) instead of setFill(false) then it toggles successfully red/blue past the 3rd time. Thanks. (Processing 2.2.1 on Windows 8.1)

// toggle shape color with mouse click
PShape p;
int toggle = 0;
void setup() {
    size(200,200, P2D);

    p = createShape();
    p.beginShape();
    p.vertex(10,0);
    p.vertex(100,50);
    p.vertex(110,80);
    p.vertex(40,100);
    p.endShape(CLOSE);

}

void draw() { 
    background(255);
    shape(p, 0, 0);
}

void mousePressed() {
    toggle = (toggle + 1) % 2;
    if (toggle == 1)
        p.setFill(color(255,0,0));
    else 
        p.setFill(false);
}
Tagged:

Answers

  • setFill(boolean fill) isn't buggy (well... I guess it depends on what you mean by buggy.)

    It's just that the boolean version of setFill just updates an object boolean, that says whether the shape should be transparent or not. (I'll get to what that has to do with anything below.)

    The generally weird thing is that setFill(int) overrides the transparency set by setFill(boolean). That's what happens on the first click.

    But now, you're trying to set a boolean that is already false, yet again, to false. Nothing changes.

    So pretty much, in the end, the only actual method call that has an effect is setFill(color(255, 0, 0)), which is the equivalent of calling the method every 2 clicks, and nothing else.

    The workaround:

    In the if statement you are setting the colour with an actual colour (the first), just add p.setFill(true);. Only then, when the fill boolean is set to false, is there a change.

    I.e:

    void mousePressed() {
      toggle = (toggle + 1) % 2;
      if (toggle == 1) {
        p.setFill(true);
        p.setFill(color(255, 0, 0));
      } else
        p.setFill(false);
    }
    
Sign In or Register to comment.