We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have made a reset sketch function. I want to trigger that function every 9000 milliseconds. I made a tigger to reset after 9000 milliseconds, but I don't know how to either reset millis() with my sketch or how to say "Every 9000 millis() resetSketch"
if (millis() > 9000){
resetSketch();
}
function resetSketch() {
millisecond = 0
balls = subset(balls, 8, 8);
for (n = 0; n < 8; n++)
append(balls, new Ball(random(width), random(height)));
}
Answers
You wouldn't reset the
millis()
function. You would keep track of the last time the sketch was reset, and then check against that value. Here's an example that draws an ellipse every second:You could also use the
frameCount
variable and the%
modulo operator to do something every X frames:@KevinWorkman Good answer, except that maybe you could convert it to p5.js (Javascript) code from Processing(Java) code. The example code in the question is in Javascript.
@wjsandbe Were you using Java, I would have recommended multithreading, but since you use Javascript, I think KevinWorkman's answer adapted to javascript is all I know.
Thanks I couldn't figure out what Modulo looked like in P5.
The code for P5.js is literally two keywords different from the code for straight Processing. And multithreading would not be a good idea in this case. Processing has built-in timing mechanisms built in. Use them.
I used the frameCount command with modulo and it works great. Thanks.
There is a library in Processing with timing mechanisms, but can you tell me where it exists in Processing core itself?
@Lord_of_the_Galaxy --
I think this is referring not to a Timer class, but to the mechanisms that tell you how many frames / milliseconds have passed since the sketch began, allowing you to create your own timers for different purposes.
I was referring to some sort of Timer class that calls some function every n milliseconds. I guess we understood the question differently.
You don't really need a
Timer
class in Processing. The functions and variables that Jeremy pointed out work perfectly with thedraw()
function.@KevinWorkman Maybe you didn't realise this, but I was not referring just to one question. What if you need multiple time triggered events? Or something the triggers irrespective of draw()? Then you probably can't rely on hard coding. Also, note that you could create a Timer class that triggers only inside draw() if needs be.
I understand what you're saying. And I don't want to take this thread off the rails any further, but again, you can do all of the above by just using the basic functions that Processing already offers. I'd advise against overcomplicating things, especially stuff that confuses novices like timing. The beauty of Processing is that it lets you do cool stuff using simple code. So might as well use that simple code.
I'm not really interested in a debate. Either way works, but I'd recommend going with the simpler route. If you prefer the more complicated option, that's fine. As with most thing in programming, there's more than one way to approach the problem. I just prefer simpler solutions.
Oh well, I guess my option is a bit of an overkill, considering that the questions isn't even about Processing(Java) but about p5.js.