1

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.

njzk2
  • 38,969
  • 7
  • 69
  • 107

1 Answers1

2
  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);
}

apodidae
  • 1,988
  • 2
  • 5
  • 9
  • OP is probably referring to `P5.js` when he states he uses the "browser version", which would explain the arbitrary lack of semicolon at times. – laancelot Apr 12 '23 at 03:39
  • @laancelot Could he run it in p5.js editor without using 'function'? I thought maybe he knew about an online editor for Processing.app which as far as I know doesn't exist. We need more clarification on what editor he used. – apodidae Apr 12 '23 at 05:27
  • Thanks for the code! Will try it out in the actual app! https://hello.processing.org/editor/ I'm using this, and the code just crashes every time with a blank error, i'm guessing this is just a worse version of the actual processing enviroment? – my_it_teacher_is_grumpy Apr 12 '23 at 06:10
  • I was unaware of this online editor, and you are correct the code above won't run in it for some reason (doesn't like background()). I promise you it will run in the actual Processing4 app (either Java or Android mode), which is a free download. We appreciate your participation here and hope that you have a rewarding coding experience. – apodidae Apr 12 '23 at 06:55
  • background(z, x, c) the way that you had it also works. I edited my answer. Still won't run in online editor with this change. – apodidae Apr 12 '23 at 07:16
  • I was likewise unaware of the Processing app being an app, and not only a browser enviroment, thank you for clarifying! And yes, it works as intended, and like a charm! And again, thank you for sharing. – my_it_teacher_is_grumpy Apr 12 '23 at 07:26