Reset variable.

edited February 2014 in Using Processing

Ok, I know that is not possible to reset millis() function. But this is not my problem.

The problem is: In my( not only in mine xD) sketch millis() start from 0 to ipothetic infinite time.

I've set a startTime variable = to millis().

Now.. when startTime reach 1000ms something happen. When startTime reach 130000ms something happen and so on..

I want that after the first launch, every 219000ms startTime turn back into 0, to repeat again all the stuff at 1000ms..130000ms etc..

Obviously in the meantime millis() goes on, so I need to reset startTime automatically after others 219000ms..

I've tried with if(),for(),and many other way but at the end of the first 219000ms nothing happened...

I try with an example:

    int startTime;
    int interval = 5000;
    void setup() {

    size(400,400);
    startTime=millis();
    }

    void draw() {

    if(startTime>2000) {
    rect(10,10,10,10);
    }

    //here I need something to make startTime turn back to 0! 
    }

There is a solution..? :(

Answers

  • Answer ✓

    Just use the modulo operator:

    int currentTime;// former startTime;
    int interval = 5000;
    
    void setup() {
        size(400, 400);
    }
    
    void draw() {
        background(#cccccc);
        if(currentTime > 2000)
            rect(10, 10, width - 20, height - 20);
        currentTime = millis() % interval; // Reset current time every interval ms
    }
    
  • o.O so simple? I really really thank you _

    (Many days passed on timed animations without find a way to make them restart have get me crazy XD)

    thanks thanks thanks ^^

  • Haha, yes, Java/Processing has a bunch of uselful operators. :D

    Btw.: Please mark my last post as answer, so others can see that this question has been answered.

  • Oh yes, made it! ;) Thanks again:)

Sign In or Register to comment.