Spawning objects using timers

edited August 2015 in Questions about Code

I am after some links to good resources about timers?

I am aiming to spawn sprites at different points of the canvas, at intervals randomly chosen between 5 - 10 seconds. Then disappear after another interval (say 10 secs).

Answers

  • A simple timer example. Search the forum for timer tag. There are a lot of examples. Some more advanced using threads... This one is very basic

    PFont font;
    String time = "000";
    int initialTime;
    int interval = 1000;//one second
    color bg = color (255);
    
    
    
    void setup()
    {
      size(300, 300);
      font = createFont("Arial", 30);
      background(255);
      fill(0);
      initialTime = millis();
        frameRate(30);// changed framerate to exemplify
    
    }
    
    void draw()
    {
      background(bg);
      if (millis() - initialTime > interval)
      {
        time = nf(int(millis()/1000), 3);
        initialTime = millis();
       bg = color (random(255), random(100), random(255));
      }
    
      text(time, width/2, height/2);
    
    
    }
    
  • Thanks for that _vk, helpful for getting my head around using timers. Hoping for a tutorial or resource looking at using timers for generating objects. Struggling to find something relevant on the forum.

  • I have found the GTimer function within the G4P library, however I am still far off from understanding how to use this function to achieve my goal.

  • edited August 2015 Answer ✓

    vk showed a perfect timer

    in the if-clause you can spawn (between { } )

    draw() runs on and on and if tests if the timer is up, spawns it, starts it again

    for the var interval you can set a random between 5 and 7 secs (5000 and 7000 millis)

    to reset the timer: initialTime = millis(); as vk has shown

    the if if (millis() - initialTime > interval) just tests whether between now (the millis()) and the starting of the timer (initialTime) enough time has passed (> interval)

    that's as simple as it gets

    please try to build this into your code.

    ;-)

  • _vk_vk
    edited August 2015

    This is getting boring, because the non OOP approach to the timers :P If you need a lot of timers than you should go for a OOP approach. As said there are a lot of examples around.

    But... here goes a more developed example, as I've seeing your try at SO have been locked :)

    This have two different random timers that spawn and delete Objects. See if you can understand how this works. Ask if you want.

    HTH

    int initialTimeAdd, initialTimeRemove ;
    int intervalAdd = 3000, intervalRemove = 6000;//one second
    color bg = color (255);
    ArrayList <Something> stuff = new ArrayList <Something>();
    
    
    void setup() {
      size(300, 300);
    
      background(255);
    
      initialTimeAdd = millis();
      initialTimeRemove = millis();
    
      frameRate(30);// changed framerate to exemplify
    }
    
    void draw() {
      background(bg);
      if (millis() - initialTimeAdd > intervalAdd) {
        initialTimeAdd = millis();
        intervalAdd = int(random(1000, 3000));
        stuff.add(new Something());
      }
    
      if (millis() - initialTimeRemove > intervalRemove) {
        initialTimeRemove = millis();
        intervalRemove = int(random(2000, 4000)); 
        stuff.remove(int(random(stuff.size())));
      }
    
      for (Something s : stuff) {
        s.display();
      }
    }
    
    
    
    
    
    class Something {
      PVector pos;
      int sz, mode;
      color c;
      Something() {
        pos = new PVector(random(width), random(height));
        sz = int(random(12, 77));
        c = color(random(255), random(255), random(255));
    
        mode = int(random(2));
      }
    
      void display() {
        noStroke();
        if (mode==0) {
          fill(c);
          ellipse(pos.x, pos.y, sz, sz);
        } else {
          fill(c);
          rect(pos.x, pos.y, sz, sz);
        }
      }
    }
    
  • I'd move all the timing stuff to the object. Create objects with a delay. Make them active after the delay is up and then have another delay that'll kill them, or reset them. In the draw loop update all the objects and draw the active ones.

    Hope that makes sense. It's a particle system, basically.

Sign In or Register to comment.