I have an object class with a function that spawns a new object, but how would I keep calling this method from main? would I need an infinite loop or maybe a do while? or should it be a recursive method? Not really sure how to implement this
Use a timer or enterFrame with a fixed period.
-- -- A dummy spawner -- local cx,cy = display.contentCenterX,display.contentCenterY local fw,fh = display.actualcontentWidth,display.actualContentHeight local l = cx-fw/2 local r = cx+fw/2 local t = cy-fh/2 local b = cy+fh/2 local mRand = math.random local function spawner() local enemy = display.newCircle( mRand(l,r), mRand(t,b), mRand(10,20) ) end
timer.performWithDelay( 333, spawner, -1 )
- OR -
local n = 10 local frameCount = 0 local function enterFrame( ) if( frameCount % n == 0 ) then spawner() end frameCount = frameCount + 1 end Runtime:addEventListener("enterFrame", enterFrame)
Use a timer or enterFrame with a fixed period.
-- -- A dummy spawner -- local cx,cy = display.contentCenterX,display.contentCenterY local fw,fh = display.actualcontentWidth,display.actualContentHeight local l = cx-fw/2 local r = cx+fw/2 local t = cy-fh/2 local b = cy+fh/2 local mRand = math.random local function spawner() local enemy = display.newCircle( mRand(l,r), mRand(t,b), mRand(10,20) ) end
timer.performWithDelay( 333, spawner, -1 )
- OR -
local n = 10 local frameCount = 0 local function enterFrame( ) if( frameCount % n == 0 ) then spawner() end frameCount = frameCount + 1 end Runtime:addEventListener("enterFrame", enterFrame)