How to use millis() to display images every x seconds?

Hi, I have an array of candy images which are randomly chosen. There are candies falling and I want to make a new one fall every 5 seconds, and I know I have to use millis() - but how would I implement it into my program? I tried using millis() like so:

int time = millis();
  if (time<5*1000)
  {
    image(goodCandy[rand], randX, goodY, randCandyW, randCandyH);
    goodY = goodY + (candySpeed * yDirCandy);
    time = millis();
  }

But it only appears for 5 seconds then goes away. I also tried:

 int time = millis();
  if (millis() - time >= 5000)
  {
    image(goodCandy[rand], randX, goodY, randCandyW, randCandyH);
    goodY = goodY + (candySpeed * yDirCandy);
    time = millis();
  }

But it didn't work.

Here's the simplified code:

    PImage [] goodCandy = new PImage [3];
    int candySpeed = 20;
    int yDirCandy = 1;
    int candyY = 10;
    int candyX = 200;
    int candyW = 187;
    int candyH = 121;
    int randCandyW = 100;
    int randCandyH = 100;

    int goodY = -200;

    int rand=(int) (2*Math.random()) +1;
    int randX = (int) (1500*Math.random())+20;

    void setup() {
    for (int i=0; i<goodCandy.length; i++) {
      goodCandy[i] = loadImage("goodCandy" + i + ".png");
    }

    void draw() {
    if (current=="play") {
    loadStuff();
    }
    }

    void loadStuff() {

      image(candy, candyX, candyY, candyW, candyH);  //original candy
      candyY = candyY + (candySpeed * yDirCandy);

      int time = millis();
      if (millis() - time >= 5000)
      {
        image(goodCandy[rand], randX, goodY, randCandyW, randCandyH);
        goodY = goodY + (candySpeed * yDirCandy);
        time = millis();
      }

      //for (int i=0; i<time; i++) {
      //  image(goodCandy[rand], randX, goodY, randCandyW, randCandyH);
      //  goodY = goodY + (candySpeed * yDirCandy);
      //  time = millis();
      //}
    }

Any ideas how I could make millis() work so I can have a random candy falling every 5 seconds? Thanks

Tagged:

Answers

  • edited June 2017

    @brianna0811 -- re:

    But it only appears for 5 seconds then goes away

    if (time<5*1000){
      // do this
    }
    

    This code says:

    • "If the time is less than 5000, do this."

    Instead:

    int elapsed = millis() - lastTime;
    if (elapsed >= 5000){
      // do this
      lastTime = millis();
    }
    

    This code says:

    • "The elapsed time is now, minus the last time."
    • "If the ellapsed time is greater than 5000, do this."
    • "After doing this, make the new 'last time' equal to now.

    Be careful not to reset lastTime except inside your loop.

  • @jeremydouglass

    Here's what i did:

    int elapsed = millis() - lastTime;
    if (elapsed >= 5000){
      image(goodCandy[rand], randX, goodY, randCandyW, randCandyH); 
      goodY = goodY + (candySpeed * yDirCandy);
      lastTime = millis();
    }
    

    However it only displays the random candy once after 5 seconds and doesn't do it EVERY 5 seconds. Is there something I'm missing that's preventing it from displaying every 5 secs?

  • Your "do this" step should not be to show the random candy, but to pick a new random candy.

    At the start, pick a random candy.
    Always show which candy is picked.
    Have five seconds passed? If so, pick a new random candy.
    
  • edited June 2017

    @brianna0811 -- No way to know without seeing more of your code.

    This sketch flashes a rectangle every 5 seconds.

    int elapsed;
    int lastTime;
    void setup() {
      frameRate(5);
    }
    void draw() {
      background(0);
      elapsed = millis() - lastTime;
      if (elapsed >= 5000) {
        rect(10, 10, 80, 80);
        lastTime = millis();
      }
    }
    
  • Compare that to this, which constantly shows the rectangle but picks a new color every five seconds:

    int elapsed;
    int lastTime;
    color c;
    
    void setup() {
      size(100,100);
      newColor();
    }
    
    void draw() {
      background(0);
      elapsed = millis() - lastTime;
      if (elapsed >= 5000) {
        newColor();  
        lastTime = millis();
      }
      fill( c );
      rect(10, 10, 80, 80);
    }
    
    void newColor(){
      c = color(random(255),random(255),random(255));
    }
    
  • this really should be a FAQ by now. (ditto collision detection and a dozen other things)

    and there are libraries that do exactly this. why does nobody use them?

Sign In or Register to comment.