We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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.
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 againfor 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 shownthe if
if (millis() - initialTime > interval)
just tests whether between now (themillis()
) 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.
;-)
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
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.