Loop 3 times and then exit window.

edited September 2016 in How To...

Hi, I'm brand new to processing and I'm stumped. I'm trying to make my program draw the image three times and then close the window without pressing anything on the keyboard or mouse. I've tried using loop() and noLoop() but I feel like I just don't understand the program enough to use them correctly. Any help would be greatly appreciated!

Tagged:

Answers

  • Loop and noLoop aren't for that.

    I would use frameCount in a condition... Something like

    If framecount % 120 < 60 then draw image
    If framecount > 360 then exit

    Check % in the reference to see how this works.

    Post your attempt.

  • edited September 2016

    maybe you can tell a bit more what you want.

    or show your code.

    draw() runs 60 times per second. So when in this sense you want to show the image 3 times, it would be over soon... and you won't see a thing.

    or do you mean an image that flashes 3 times?

    void setup() {
      size(444, 444);
    }
    
    void draw() {
      // show image here 
      if (frameCount > 360) 
        exit();
    }
    
  • Basically, I have three ellipses overlapping that are different colours. The ellipses gradually appear and once they reach full colour a crosshair appears in the centre, they vanish and start to reappear again. I'm trying to get them to appear three times and then the window closes on its own.

  • @emburke -- you should definitely share your code -- this will make it much easier to help you.

  • edited September 2016

    I've created a simplified version of my code as I can't seem to be able to post the original code here nicely. I hope this helps.

    int iAlpha=0;
    
    
    void setup()
    {
      size(800,800);
    }
    
    void draw()
    {
      background(180);
      stroke(0);
      strokeWeight(2);
      fill(0,255,0,iAlpha);
      ellipse(width/2,height/2,200,200);
    
      iAlpha+=1;
    
      if(iAlpha>255)
      {
        delay(2000);
        iAlpha=0;
      }
    
    }
    
  • Edit post, highlight code, press Ctrl-o

  • edited September 2016

    Thanks

  • Answer ✓

    You should add a new variable that counts how many times you've reached the if(iAlpha>255) condition. Once this counter hits three, you can call exit() to quit your sketch

    Lines of code you will need to add:

    int counter = 0;
    

    And:

    if(counter == 3){ exit(); }
    

    And:

    counter++;
    
  • The window didn't close, and the code just kept running. Does counter++; go after the if statement?

  • Nevermind, I put it somewhere else and it worked! THANK YOU!!

Sign In or Register to comment.