Timer with random seconds ¿How to update the random seconds?

I have a timer “tmr_sendCesta” which must be called each x seconds between 1 and 3 seconds. The problem is the timer “tmr_sendCesta” is called only one time, and the random seconds is never updated. I need to call the function “createCesta” each x seconds randomly.

Any idea how to do it?

 

function createCesta()
cesta = display.newImageRect(“cesta.png”, 100, 55)
cesta.x = -110
cesta.y = screenH - 110
cesta.name = “cesta”
physics.addBody( cesta, physicsData:get(“cestaSmall”))
grupoCesta:insert(cesta)
transition.to(cesta, {time = 4000, x = screenW + 110})
end

function scene:enterScene( event )
local group = self.view
physics.start()
Runtime:addEventListener(“touch”, touchScreen)
Runtime:addEventListener( “collision”, onCollision )

tmr_sendCesta = timer.performWithDelay((math.random(1000, 3000), randomCesta, 0)
end

This is because in timer.performWithDelay, it will take the first random-value.

If you want the ‘randomCesta’ to load randomly (every 1-3seconds), then you must have a timer.performWithDelay also in ‘randomCesta’. Something like this :

local function randomCesta() print( "randomCesta fired!" ) timer.performWithDelay( math.random(1000,3000), randomCesta, 1 ) -- Only do this ONCE end timer.performWithDelay( math.random(1000,3000), randomCesta, 1 ) -- Only do this ONCE

Very nice. Thank you!!

This is because in timer.performWithDelay, it will take the first random-value.

If you want the ‘randomCesta’ to load randomly (every 1-3seconds), then you must have a timer.performWithDelay also in ‘randomCesta’. Something like this :

local function randomCesta() print( "randomCesta fired!" ) timer.performWithDelay( math.random(1000,3000), randomCesta, 1 ) -- Only do this ONCE end timer.performWithDelay( math.random(1000,3000), randomCesta, 1 ) -- Only do this ONCE

Very nice. Thank you!!