Redraw() function not working, assistance required.

edited April 2017 in Questions about Code

Hello! I'm quite new to processing and still figuring out how some of the basics work. I'm trying to use the redraw() function in this sketch, in order to redraw the screen. However, it's not redrawing anything. I have a println() function in the void setup() which shows a value for the x variable, which randomises each time the sketch is run. Whenever I press a key, I'm thinking that shouldn't the value of the x variable change in the console, which would then determine what the colour of the ellipse would be in the sketch? But, nothing is happening to the sketch. :(.

float x = int (min(1,10));

void setup() {
  size(100, 100);
  smooth();
  println(x);
}

void draw() {
  if (x > 5) {
    ellipse(50, 50, 50, 50);
  } else {
    rect(50, 50, 50, 50);
  }
}

void keyPressed() {
  redraw();
}
Tagged:

Answers

  • edited April 2017 Answer ✓

    Do you think redraw() is going to start the sketch over, and thus run setup() again? It's not. What redraw() does is cause draw() to run again - that's all. And since your sketch is constantly running draw() again anyway (because you haven't called noLoop()to stop it from doing that), your call to redraw() is pointless.

    It's also worth mentioning that you're not changing the value assigned to x at all. And the value that you are assigning to x, int(min(1,10)) is always going to be 1 (because 1 is smaller than 10, min(1,10) is 1, and int(1) is still 1). Maybe you should try a call to random() instead?

    float x = random(10);
    
    void setup() {
      size(100, 100);
      println(x);
    }
    
    void draw() {
      background(0);
      if (x > 5) {
        ellipse(50, 50, 50, 50);
      } else {
        rect(25, 25, 50, 50);
      }
    }
    
    void keyPressed() {
      x = random(10);
      println(x);
    }
    
  • Yep! My bad on the min() I was experimenting with other things as well. Thanks for the help.

Sign In or Register to comment.