Hi I am looking at this: CocosLearning Scheduling and I am interested in scheduling a function call forever with a time gap between calls. But I do not want a fixed time gap, I am trying to figure out how I can call it at random times.
So I am using Math.floor((Math.random() * 3) + 1) to generate a random number between 1-3 seconds and I would like to use that to schedule the call.
Logic behind this:
set time using Match.random()
countdown the time (in seconds) till 0 then call function
set time again (new random time) and countdown to call function again
repeat forever
Is there a way to do that?
Example:
this.schedule( this.UpdateFunction, 1.0 );
UpdateFunction: function( dt ) {
// code to run
},
Basically, use scheduleOnce to schedule the function you want called and then re-use scheduleOnce whenever the function is called. Don’t know if there’s a better way, but it works for me.
Though the scheduleOnce didn’t work for me, I had to use schedule in order to get it to run more than once. It would run the UpdateFunction once but then that was it, even though it called scheduleOnce again, it didn’t run it.
How about using an action sequence consisting of a delay action and a function call action. The function to be called does stuff, creates the same sequence again and starts over.
No fancy schedulers needed(no scheduling/unscheduling logic)