Spawn Function in Corona

Okay so I kind of am almost there but need a little push:

My dustbins spawn but there are a few to begin with like I want and then after about 30 seconds of the numbers of dustbins increasing there are 1000’s all clumped together… What can i do to alter my function so that its only ever a few at a time:

function spawnDustbin() dustbin = {} for i = 1,800 do dustbin[i] = display.newImage("dustbin.png") dustbin[i].xScale = 0.55 dustbin[i].yScale = 0.55 dustbin[i].y = 555 dustbin[i].x = (math.random(1000,1500) \* i) dustbin[i].speed = 4 physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=30,} ) dustbin[i].enterFrame = moveDustbin Runtime:addEventListener("enterFrame", dustbin[i]) end end  

and the movedustbin function simply moves the dustbin in the negative x direction:

function moveDustbin(self,event) if self.x \< -2560 then self.x = 2560 else self.x = self.x - val end end &nbsp;

First 20ish seconds: http://i.stack.imgur.com/7iEeP.png

After 20 seconds: http://i.stack.imgur.com/aae0D.png

Thank you very much James

That “spawnDustbin” function above is spawning 800 dustbins every frame. That will really crush your performance, and I am pretty certain you don’t want to create that many objects every frame. 

You might want to do something like this: 

local dustbin = {} local i = 0 local function spawnDustbin() i = i+1 dustbin[i] = display.newImage("dustbin.png") dustbin[i].xScale = 0.55 dustbin[i].yScale = 0.55 dustbin[i].y = 555 dustbin[i].x = (math.random(1000,1500) \* i) dustbin[i].speed = 4 physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=30,} ) dustbin[i].enterFrame = moveDustbin Runtime:addEventListener("enterFrame", dustbin[i]) end timer.performWithDelay(500, spawnDustbin, 10)

YMMV but that will spawn 10 dustbins in 5 seconds. 

You are without a doubt a god among men. 

I could kiss you.

Thank you very much,

James 

With so many objects, you might be better off adding all of them to a display group, then iterating through the group to move the objects with just the one enter frame or timer function. If you search youtube for ‘corona sdk flappy bird’ you will find my 7 part code walk through that explains how to achieve this. The green pipes in flappy bird have to be spawned and then move across the screen from right to left.

That “spawnDustbin” function above is spawning 800 dustbins every frame. That will really crush your performance, and I am pretty certain you don’t want to create that many objects every frame. 

You might want to do something like this: 

local dustbin = {} local i = 0 local function spawnDustbin() i = i+1 dustbin[i] = display.newImage("dustbin.png") dustbin[i].xScale = 0.55 dustbin[i].yScale = 0.55 dustbin[i].y = 555 dustbin[i].x = (math.random(1000,1500) \* i) dustbin[i].speed = 4 physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=30,} ) dustbin[i].enterFrame = moveDustbin Runtime:addEventListener("enterFrame", dustbin[i]) end timer.performWithDelay(500, spawnDustbin, 10)

YMMV but that will spawn 10 dustbins in 5 seconds. 

You are without a doubt a god among men. 

I could kiss you.

Thank you very much,

James 

With so many objects, you might be better off adding all of them to a display group, then iterating through the group to move the objects with just the one enter frame or timer function. If you search youtube for ‘corona sdk flappy bird’ you will find my 7 part code walk through that explains how to achieve this. The green pipes in flappy bird have to be spawned and then move across the screen from right to left.