Search code examples
processing

Code is not changing background every half a second


The title sums it up. I'm also a newbie to processing, so please, explain my error to me like I'm 5.

void setup(){
    size(500,500);
    int(q=1)
}

void draw(){
    if(q>0){
        float(z=random(255));
        float(x=random(255));
        float(c=random(255));
        background(z,x,c)
    }
    delay(500)
}

I use the browser version, if that matters.

I tried making an "if" loop, a "while" loop, I wanted it to change background color every 1/2 seconds, but instead it just changes once and then crashes. I use the browser version, if that matters.


Solution

    1. Each line of code needs to be followed by a semicolon.
    2. Make 'q' a global variable since you want to reference it in another function (ie draw()). Variables defined inside a function can't be used outside of that function.
    3. Remove all unnecessary parentheses.

    The following code should run without errors. Keep reading and writing code.

    int q = 1;
    
    void setup() {
      size(500, 500);
    }
    
    void draw() {
      if (q > 0) {
        float z = random(255);
        float x = random(255);
        float c = random(255);
        background(z, x, c);
      }
      delay(500);
    }