In a previous post, I learned how to create a repeating action, a scooter zipping across the screen:
local randTime = math.random( 500, 1000 ) function scooterFunction() local scooter = display.newImage("scooter.png") scooter.x = -100 scooter.y = math.random(1,1024) transition.to(scooter, {time = 1500, delay = 500, x = scooter.x + 968})end timer.performWithDelay(randTime, scooterFunction, 0)[/code]So if the randTime variable comes back 500, for example, this code causes a new scooter to be spawned every half second. But this got me wondering: What if I want to randomize the interval between spawnings? So I tried nesting the timer.performWithDelay command within a function, like so:local randTime = math.random( 500, 1000 )function scooterFunction() local scooter = display.newImage("scooter.png") scooter.x = -100 scooter.y = math.random(100,924) transition.to(scooter, {time = 1500, delay = 0, x = scooter.x + 968})endfunction scooterTriggerFunction()timer.performWithDelay(math.random(100,10000), scooterFunction, 1)endtimer.performWithDelay(randTime, scooterTriggerFunction, 0)[/code]This randomized the delay on each firing, but didn't give me the true random quality I'm interested in, as the spawning is still tied to the 'ol timer.performWithDelay(randTime, Function , 0)[/code]command, which always ends up firing at regular intervals. What I would like is a command that will fire after half a second, again after 2.9 seconds, again after 10 seconds, again after 4.3 seconds, etc, etc. Any ideas?As a footnote, let it be said that this post was created more out of curiosity than necessity, but sometimes curiosity gets the better of me:) [import]uid: 79394 topic_id: 14043 reply_id: 314043[/import]
It’s quite simple really
[lua]local function randomShoot()
timer.performWithDelay(math.random(200, 400), randomShoot)
– your code here
end
randomShoot()[/lua] [import]uid: 52103 topic_id: 14043 reply_id: 51722[/import]
Embarassingly so:)
Thank you!
Steven [import]uid: 79394 topic_id: 14043 reply_id: 51728[/import]