How can you make a draw loop a specific amount of times?

edited February 2015 in Questions about Code

I have a for loop and want it to create a random drawing 7 times without having to use:

void mousePressed() { loop(); // Holding down the mouse starts the loop }

Answers

  • edited February 2015

    don't use noLoop() in the first place

    instead have a global var howMany

    int howMany = 0;

    and in draw() say

    howMany++;
    
    if (howMany>7) 
       noLoop(); 
    
  • edited February 2015

    @Chrisir I can't seem to get that to work properly in my code

    void setup()
    {
      //size of canvas
      size(600, 750);
      //background is black
      background(0);
      //stroke color is white
      stroke(255);
      //stroke is smooth and not pixelated
      smooth();
    }
    
    void draw()
    {
      BubbleDraw();
    }
    
    void BubbleDraw()
    {
      //displacing the circles in a group around the middle
      translate(width/2, height/2);
      int drawing = 0;
      for (int i = 0; i < 15; i++)
      {
        for (int j = 0; j < 15; j++)
        {
          //circles have no fill (press picture before page turns white)
          fill(0);
          //the angles in which the circles rotate are from -90 to 90
          float angle = random (-90, 90);
          //changes degrees to radians
          rotate (radians (angle));
          //circles made at 0,0 progressively get larger
          ellipse(0, 0, i+j, i+j);
          //moves the circles individually 
          translate(0, 25);
          drawing++;
        }
    
        if(drawing>7)
        { 
          noLoop();
        }  
      }
    }
    
  • here

    int drawing = 0;
    
    void setup()
    {
      //size of canvas
      size(600, 750);
      //background is black
      background(0);
      //stroke color is white
      stroke(255);
      //stroke is smooth and not pixelated
      smooth();
    }
    
    void draw()
    {
      stroke(drawing*20+60, 0, 0);
      BubbleDraw();
      drawing++;
      if (drawing>7)
      { 
        noLoop();
      }
      println (drawing);
    }
    
    void BubbleDraw()
    {
      //displacing the circles in a group around the middle
      translate(width/2, height/2);
    
      for (int i = 0; i < 15; i++)
      {
        for (int j = 0; j < 15; j++)
        {
          //circles have no fill (press picture before page turns white)
          fill(0);
          //the angles in which the circles rotate are from -90 to 90
          float angle = random (-90, 90);
          //changes degrees to radians
          rotate (radians (angle));
          //circles made at 0,0 progressively get larger
          ellipse(0, 0, i+j, i+j);
          //moves the circles individually 
          translate(0, 25);
        }
      }
    }
    
    • you needed to make it a global var (define it before setup so it's valid everywhere)

    • say the stuff in draw() not in BubbleDraw()

    ;-)

  • That makes so much sense thank you so much! @chrisir

  • great!

    ;-)

Sign In or Register to comment.