Well your code will spawn 10 objects over 6 seconds… And then the timer ends… I want it to repeat its self infinite until I cancel the timer.
How fast do you want them to spawn?
I would like to change the speed of how fast they spawn.
There are a couple of different approaches to this. If you want to use a timer, then you can only setup one timer at a time and at the end of spawnCircles, when you know how fast you want to spawn the next one, call another single timer for the next spawn time.
You can also use a Runtime “enterFrame” listener which fires 30 or 60 times per second. In that run time you can keep track of how much time has elapsed and when you hit the time you need, call the spawn function.
Rob
I’m getting confused now haha, maybe let’s try some code and maybe I’ll see what works?
local spawnSpeed = 2000 local spawnCircles = function() local Fall = math.random(display.contentWidth \* 0, display.contentWidth \* 1.0) circle[bCircle] = display.newImageRect( "circle.png", 50, 50 ) circle[bCircle].x = Fall circle[bCircle].y = -100 sceneGroup:insert(circle[bCircle]) physics.addBody( circle[bCircle], "dynamic", {bounce = 0} ) physics.setGravity( 0, 4.0) circle[bCircle].collision = onCollision circle[bCircle]:addEventListener( "collision", circle[bCircle] ) circle[bCircle].value = bCircle bCircle = bCircle + 1 spawnSpeed = spawnSpeed - 100 if spawnSpeed \< 300 then spawnSpeed = 300 end circleTimer = timer.performWithDelay( spawnSpeed, spawnCircles, 1 ) end circleTimer = timer.performWithDelay( spawnSpeed, spawnCircles, 1 )
I don’t know how you want to determine how to change the spawn time, that’s on you.
I think he wants to spawn bursts of circles? So, say, every 2 seconds spawn 10 circles, infinitely?
If so, just do a for loop. It may be worth investing in a Lua book or some further study of the basics.
for i=1,10 do -- spawn a circle here end
Yes Michael, i think he is refering to spawning “bursts of circles”
This should work, now I’m not 100% confident with the performWithDelay inside another performWithDelay, i think I had some problems before with a similar approach, but I currently cant test it.
local physics = require "physics" physics.start() physics.setGravity( 0, 4.0) local circle = {} local bCircle = 1 --bursts, in this case 1 circle each 100ms, 10 times. local circlesBurst\_ammount = 10 local circlesBurst\_timer = 100 --spawns each on of above bursts every 2000ms, 2 times local spawnTimer = 2000 local spawnIterations = 2 local function spawnCircles () local Fall = math.random( 0 , display.contentWidth \* 1 ) circle[bCircle] = display.newImageRect( "circle.png", 50, 50 ) circle[bCircle].x = Fall circle[bCircle].y = -100 sceneGroup:insert(circle[i]) physics.addBody( circle[i], "dynamic", {bounce = 0} ) circle[bCircle].collision = onCollision circle[bCircle]:addEventListener( "collision", circle[bCircle] ) circle[bCircle].value = bCircle bCircle = bCircle + 1 end local circleSpawn = timer.performWithDelay( spawnTimer , function() timer.performWithDelay( circlesBurst\_timer, spawnCircles, circlesBurst\_ammount);end, spawnIterations)
I would do something like:
local function spawnCircle( ... ) ... end local function spawnBurst( speed, count ) -- might want to save the timer to cancel it... timer.performWithDelay( speed, spawnCircle, count ) end local function spawnLevel( burstSpeed, burstCount ) timer.performWithDelay( burstSpeed, function() spawnBurst( 300, 10); end, burstCount end spawnLevel( 10000, 20 )
or something like that.
Rob