Making a for loop with timer

int delay = 2000;

void setup() {
  size(200, 200);
  time = millis();
  fill(#989696);
  stroke(#747373);
  rectMode(CENTER);
  rect(width - 70, 35, 120, 40);
}

void draw() {
  for (int i = width - 130; i < width - 10; i += 1) {
    if (millis() - time >= delay) {
      stroke(#00B4A7);
      line(i, 25, i, 55);
      println("n");
      time = millis();
    }
  }
} 

I am trying to essentially draw a line every two seconds, and then moving that line over one pixel and repeating. Any help would be appreciated :slight_smile:

1 Like

Like this… (Cheers Dan!)

int delay = 2000;
Timer timer; 
int i;

void setup() {
  size(400, 200);
  timer = new Timer(1000);    // Create a timer that goes off every 1 second
  timer.start();
  fill(#989696);
  stroke(#747373);
  rectMode(CENTER);
  rect(width - 70, 35, 120, 40);
  i = width - 130;
  
}

void draw() {
  if (timer.isFinished()){
      stroke(#00B4A7);
      line(i, 25, i, 55);
      println("Timer", i);
       i++;
      timer.start();
    }
       
  if (i > width - 10){
    i = width - 130; //reset to your starting point
  }else{
   
  }
}





// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 10-4: The raindrop catching game

class Timer {

  int savedTime; // When Timer started
  int totalTime; // How long Timer should last

  Timer(int tempTotalTime) {
    totalTime = tempTotalTime;
  }

  void setTime(int t) {
    totalTime = t;
  }

  // Starting the timer
  void start() {
    // When the timer starts it stores the current time in milliseconds.
    savedTime = millis();
  }

  // The function isFinished() returns true if 5,000 ms have passed. 
  // The work of the timer is farmed out to this method.
  boolean isFinished() { 
    // Check how much time has passed
    int passedTime = millis()- savedTime;
    if (passedTime > totalTime) {
      return true;
    } else {
      return false;
    }
  }
}
2 Likes