Help Needed Incresing Number of Objects Spawned Overtime!

I am having a difficult time trying to figure out how increase the number of asteriods spawned overtime. I’m trying to decrease the spawnTime by 10 miliseconds every 10 seconds, then have the timer repeat 30 times before she finishes.

Thanks for your help :slight_smile:

local comet, comet2, comet3, bounds, imageTable, childGroup, spawnTimer, cometTimer

local physics = require( “physics” )

local minimumX = 50

local maximumX = display.viewableContentWidth - 50

local minimumY = 50

local maximumY = display.viewableContentHeight - 50

local P = {}

     bounds = {

     xMin = 0,

     xMax = display.viewableContentWidth,

     yMin = -300,

     yMax = -100,

     spawnTime = 800,

     spawnOnTimer = 1,

     spawnInitial = 0

}

imageTable = { “comet2.png”, “comet3.png” }

childGroup = display.newGroup()

function P.spawnItem()

a = math.random( 1, 2 )

     comet = display.newImageRect( imageTable[a], 200, 200 )

     comet.x = math.random( bounds.xMin, bounds.xMax )

     comet.y = math.random( bounds.yMin, bounds.yMax )

     physics.addBody( comet, { density=1.0, bounce=0.1, radius=85, filter=cometCollisionFilter } )

     comet.angularVelocity = -200

     comet.gravityScale = 0.8

     comets[#comets + 1] = comet

     childGroup:insert( comet )

     comet = display.newImageRect( imageTable[a], 180, 180 )

     comet.x = math.random( bounds.xMin, bounds.xMax )

     comet.y = math.random( bounds.yMin, bounds.yMax )

     physics.addBody( comet, { density=1.0, bounce=0.1, radius=75, filter=comet2CollisionFilter } )

     comet.angularVelocity = 375

     comet.gravityScale = 1.0

     comets[#comets + 1] = comet

     childGroup:insert( comet )

     comet = display.newImageRect( imageTable[a], 150, 150 )

     comet.x = math.random( bounds.xMin, bounds.xMax )

     comet.y = math.random( bounds.yMin, bounds.yMax )

     physics.addBody( comet, { density=1.0, bounce=0.1, radius=60, filter=comet3CollisionFilter } )

     comet.angularVelocity = -250

     comet.gravityScale = 0.9

     comets[#comets + 1] = comet

     childGroup:insert( comet )

     sceneGroup:insert( childGroup )

     return comet

end

function P.removeComet()

     if ( table.maxn( comets ) >= 0 ) then

          for i=1, table.maxn( comets ) do

               if comets[i].y > maximumY + 200 then

                    comets[i]:removeSelf()

                    comets[i] = nil

                    table.remove( comets, i )

                    return

               end

          end

     end

end 

Runtime:addEventListener( “enterFrame”, P.removeComet )

function P.increaseComets()

     bounds.spawnTime = bounds.spawnTime -10

end

spawnTimer = timer.performWithDelay( bounds.spawnTime, P.spawnItem, 0 )

cometTimer = timer.performWithDelay( 10000, P.increaseComets, 30 )

I’m presuming the problem is that the time is not reducing, yes?

I think what’s happening is that when you first set up the spawnTimer, it remembers bounds.spawnTime as it is then and reuses the same value over and over.

I think this should work for you:

local function newSpawnTimer() P.spawnItem() spawnTimer = timer.performWithDelay( bounds.spawnTime, newSpawnTimer ) end spawnTimer = timer.performWithDelay( bounds.spawnTime, newSpawnTimer ) cometTimer = timer.performWithDelay( 10000, P.increaseComets, 30 )

Thank you very much hasty, works perfect!

I’m presuming the problem is that the time is not reducing, yes?

I think what’s happening is that when you first set up the spawnTimer, it remembers bounds.spawnTime as it is then and reuses the same value over and over.

I think this should work for you:

local function newSpawnTimer() P.spawnItem() spawnTimer = timer.performWithDelay( bounds.spawnTime, newSpawnTimer ) end spawnTimer = timer.performWithDelay( bounds.spawnTime, newSpawnTimer ) cometTimer = timer.performWithDelay( 10000, P.increaseComets, 30 )

Thank you very much hasty, works perfect!