Well i got the error to stop…
display.setStatusBar( display.HiddenStatusBar ) local x\_location = display.contentCenterX local y\_location = display.contentCenterY local physics = require ( "physics" ) physics.start() local bg = display.newRect( x\_location, y\_location, 320, 480) bg:setFillColor(0,0,1) local box=display.newRect(x\_location , y\_location, 10,480) box:setFillColor(1,0,0) physics.addBody(box,"static", {isSensor= true}) local y= math.random(80,180) local badfish = display.newCircle( x\_location+ 100, y, 17); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) local function clearFish(event) if (event.phase == "ended" and event.other==badfish) then display.remove(badfish) badfish=nil if badfish ~= nil then local z= math.random(80,180) badfish = display.newCircle( x\_location + 100, z, 17); local table = { density=1, bounce=0, friction=0, radius=17 } timer.performWithDelay(100, function () physics.addBody(badfish, "dynamic", table) end,1) function setter() badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer.performWithDelay(50,setter,1) end end end box:addEventListener( "collision", clearFish )
The error happened because this line of code
if badfish == nil then
was supposed to be this
if badfish ~= nil then
BUTTTTTTTTTTTTT
When i was typing my answer your replied with the explanation and i instantly knew your problem…
So heres the new code that does what you wanted it to do… ( From what i understood… )
display.setStatusBar( display.HiddenStatusBar ) local x\_location = display.contentCenterX local y\_location = display.contentCenterY local timer1 local timer2 local physics = require ( "physics" ) physics.start() local bg = display.newRect( x\_location, y\_location, 320, 480) bg:setFillColor(0,0,1) local box=display.newRect(x\_location , y\_location, 10,480) box:setFillColor(1,0,0) physics.addBody(box,"static", {isSensor= true}) local y= math.random(80,180) local function spawnFishOnce() local y= math.random(80,180) badfish = display.newCircle( x\_location+ 100, y, 17 ); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer1 = timer.performWithDelay( 1, spawnFishOnce, 1 ) local function clearFish(event) if ( event.phase == "ended" and event.other == badfish ) then display.remove(badfish) --badfish=nil local function spawnNew() print("hi") local y= math.random(80,180) badfish = display.newCircle( x\_location+ 100, y, 17 ); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer2 = timer.performWithDelay( 50, spawnNew, 1 ) end end box:addEventListener( "collision", clearFish )
So to explain what i did…
So when the game starts then i spawned a fish once and then that function stops…(spawnFishOnce)
Now when the fish goes passed the line and disappears the timer2 starts and waits 50 milliseconds and fires the spawnNew function and places the fish on a new y axis every time…
Good Luck!!