Problem with Enemy spawning more after a certain time

Hi everyone I have a game publish already with Corona my 5th app made. My problem is I’m trying to add something to it. What I’m trying to add is have enemies spawn more frequent after a certain time. This is how I have the code

  
---Below here is were the enemy spawns between 1 to 5 seconds---  
  
local function enemies5 (event)  
local sheet1 = sprite.newSpriteSheet( "fly.png", 60, 36 )  
local heroset = sprite.newSpriteSet (sheet1, 1, 8)  
local enem5 = sprite.newSprite (heroset)   
sprite.add (heroset, "hero", 1, 8, 200, 0)  
enem5:prepare("hero")  
enem5:play()  
enem5.y = math.random(30, 290)  
enem5.x = 600  
enem5.isFixedRotation = true  
physics.addBody (enem5, {bounce=0.6, isSensor = true})  
transition.to (enem5, {time = 5000, x=-50})  
enem5.hit = "enem5"  
enem5.hits = 0  
localGroup:insert(enem5)  
end  
bad5 = timer.performWithDelay(math.random(1000,5000), enemies5, 0)   
  
--- Below here the code is were I cancel timer bad5 after the player surpass dis 10 which dis means distance. So after distance 10 the enemy now spawns every 1 second. ---  
  
local function fast5()  
if dis \>= 10 then  
timer.cancel (bad5)  
bad5a = timer.performWithDelay(1000, enemies5, 0)  
end  
end  
Runtime:addEventListener("enterFrame", fast5)  
  
---Below here the code is when the player life is less than 0 I cancel all timer, and remove listeners, after 1 second the scene is change--  
  
local function dead ()  
if Life \<= 0 then  
timer.cancel (bad5a)  
Runtime:removeEventListener("enterFrame", fast5)  
timer.cancel (bad5)  
director:changeScene ("over")  
end  
timer.performWithDelay (1000,fixit,1)  
end  
end  
heli:addEventListener ("collision", dead)  

My problem with this is the enemies always appear in the over scene. When they appear I get an error saying

…ttas/Desktop/Tiny Lemon Games/Deceased City/game.lua:17: attempt to call method ‘insert’ (a nil value)
stack traceback:
[C]: in function ‘insert’
…ttas/Desktop/Tiny Lemon Games/Deceased City/game.lua:17: in function ‘_listener’
?: in function <?:514>
?: in function <?:215>

This error corresponds to the code above
[import]uid: 17058 topic_id: 24830 reply_id: 324830[/import]

Sounds like it may have something to do with fixit() being called 1 second after the scene change.

Once you’ve changed scene, fixit() is then trying to access enem5 which is nil at that point.

Maybe set a flag to only execute enemies() if the flag is true.
Try adding “local flag” at line 2.

Between lines 3 and 4 put “if flag == true then” and the corresponding end at the close of the function.

Then between lines 34 and 35 in the dead() add “flag = false”

Hope that helps.

Dan [import]uid: 67933 topic_id: 24830 reply_id: 100729[/import]