How can I do timing animations?

edited December 2015 in How To...

Hi there, I would know how can I do timing animations in processing. For example: in an animation, 10 seconds after the sketch has been run, a CAR comes in from the left side of the screen and goes out from right . Then, at 20th second, a PLANE comes in from the left side and goes out from right . At 30th second, BLUE car comes in and goes out . And so on....

Answers

  • Answer ✓

    you can use millis()

    Store the millis at end of setup() in a var startTime

    Then calculate the time since that:

    passedTime = startTime - millis() ;

    if this value is >= 10 etc. trigger the event

  • Thanks Frank, but could you show me in a simple example with code

  • edited December 2015 Answer ✓
    int startTime;
    int x = -50;
    
    void setup() {    
    size(250, 250);
    startTime = millis();
    }
    
    void draw() { 
    background(0);
    if (millis() - startTime > 10000){
        animation();
        } 
    
        println(millis());
    }
    
    void animation(){
    rect(x, height/2, 50, 50);
    x+=5;
    }
    
  • Thanks for the answer Ater, but after 10 second, how can I cancel this loop, and for example, at the 11th second an ellipse come in ?

  • edited December 2015

    You could make an array of objects TimeEvent - a class which you then have to write

    Each object holds when to start and when to stop and what

  • Answer ✓

    Just have void animation2() and do the same, if you want to stop previous animation on 11s second, line 11 should be if (millis() - startTime > 10000 && millis() - startTime < 11000)

  • Thanks a lot guys

Sign In or Register to comment.