Items spawning after scene change

So I am making a game where items spawn and fall to the bottom of the screen. When then scene changes, IE going to the “You Lose” screen, one item spawns and stays on the top of the screen, and I get the error “Attempted to call method ‘Insert’”

Anyone have any ideas? Here is the spawn code.

I think that the spawn_Drop function isn’t ending when I change scenes, but I have the timer canceling in the KILL_TIMERS() function, so Im at a loss.

function spawn_Drop (event)

enemy_Drop = display.newImage( “Assets/game_Screen/FKdrop.png” )

enemy_Drop.x = enemy_Ketchup.x + 70

enemy_Drop.y = 70

enemy_Drop.Name = “drop”

physics.addBody(enemy_Drop, “dynamic”)

group:insert(enemy_Drop)

end

spawn_Timer = timer.performWithDelay( spawn_Speed, spawn_Drop, 0)

Did you use a timer cancelling function like I suggested here? If so, can you provide that code so we can take a look at what the problem might be?

function scene:enterScene( event ) local group = self.view --Timers------------------------------------------------------------------------------ function game\_Time () time = time + 1 print("Time: "..time) end game\_Time\_Timer = timer.performWithDelay( 1000, game\_Time, 0 ) --Spawn Harder----------------------------------------------------------------------------- function drop\_Faster () gravity\_Speed = gravity\_Speed + 7 physics.setGravity( 0, gravity\_Speed ) print("Gravity Speed: "..gravity\_Speed) end drop\_Faster\_Timer = timer.performWithDelay( 5000, drop\_Faster, 2 ) --Characters------------------------------------------------------------------------------ function move\_Ketchup () ketchup\_X = math.random(20, 240 ) transition.to( enemy\_Ketchup, {x = ketchup\_X, time = 850} ) end move\_Ketchup\_Timer = timer.performWithDelay( 900, move\_Ketchup, 0 ) function move\_Fry (event) if ( event.phase == "moved" ) then fry\_Physical.x = player\_Fry.x - 60 hand\_Physical\_Diagonal.x = player\_Fry.x + 25 hand\_Physical\_Flat.x = player\_Fry.x + 70 player\_Fry.x = event.x end end Runtime:addEventListener( "touch", move\_Fry ) --Drops------------------------------------------------------------------------------ function spawn\_Drop (event) enemy\_Drop = display.newImage( "Assets/game\_Screen/FKdrop.png" ) enemy\_Drop.x = enemy\_Ketchup.x + 70 enemy\_Drop.y = 70 enemy\_Drop.Name = "drop" physics.addBody(enemy\_Drop, "dynamic") group:insert(enemy\_Drop) end spawn\_Timer = timer.performWithDelay( spawn\_Speed, spawn\_Drop, 0) local function removeOffscreenItems() for i = 1, #all\_Drops do local oneDrop = all\_Drops[i] if (oneDrop and oneDrop.y) then if oneDrop.y \> screenHeight then oneDrop:removeSelf() table.remove( all\_Drops, i ) print("delete") lives = lives - 1 print("Lives: "..lives) audio.play(missed\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end end end end end Runtime:addEventListener( "enterFrame", removeOffscreenItems ) --Collisions------------------------------------------------------------------------------ function onCollision( event) if ( event.phase == "began" ) then print( "began: " .. event.object1.Name .. " & " .. event.object2.Name ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.Name .. " & " .. event.object2.Name ) end if ( event.phase == "began" ) then if (event.object1.Name == "fry" and event.object2.Name == "drop") then system.vibrate() print("Lives: "..lives) CallKillPlayerEndGame(event.object2); audio.play(ding\_Sound, {loops = 0}) drops\_Caught = drops\_Caught + 1 drops\_Caught\_text.text = "Drops Caught: "..drops\_Caught elseif (event.object1.Name == "drop" and event.object2.Name == "fry") then system.vibrate() print("Lives: "..lives) CallKillPlayerEndGame(event.object1); audio.play(ding\_Sound, {loops = 0}) drops\_Caught = drops\_Caught + 1 drops\_Caught\_text.text = "Drops Caught: "..drops\_Caught --enemy\_Drop:removeSelf() elseif (event.object1.Name == "hand\_flat" and event.object2.Name == "drop") then lives = lives - 1 print( "Lives: "..lives ) CallKillPlayerEndGame(event.object2); REMOVE\_SPLAT() hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x hand\_Splat.y = event.object2.y + 40 group:insert(hand\_Splat) audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end elseif (event.object1.Name == "drop" and event.object2.Name == "hand\_flat") then lives = lives - 1 print( "Lives: "..lives ) CallKillPlayerEndGame(event.object1); REMOVE\_SPLAT() hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x hand\_Splat.y = event.object2.y + 40 group:insert(hand\_Splat) audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end elseif (event.object1.Name == "hand\_diagonal" and event.object2.Name == "drop") then hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x + 20 hand\_Splat.y = event.object2.y + 20 hand\_Splat.rotation = -45 group:insert(hand\_Splat) REMOVE\_SPLAT() lives = lives - 1 print( "Lives: "..lives) CallKillPlayerEndGame(event.object2); audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end elseif (event.object1.Name == "drop" and event.object2.Name == "hand\_diagonal") then hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x + 20 hand\_Splat.y = event.object2.y + 20 hand\_Splat.rotation = -45 group:insert(hand\_Splat) REMOVE\_SPLAT() lives = lives - 1 print( "Lives: "..lives ) CallKillPlayerEndGame(event.object1); audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end end end end Runtime:addEventListener("collision", onCollision) function CallKillPlayerEndGame(enemytoKill) --Used a timer to execute to kill the enemy - because you can not remove an object during the --Collision so we execute a small delay to allow the collision to complete. timer.performWithDelay( 1, function() enemytoKill:removeSelf() end ) end function REMOVE\_SPLAT () timer.performWithDelay( 800, function() hand\_Splat:removeSelf() end ) end --Level 2------------------------------------------------------------------------------------------ function go\_to\_Level2 () storyboard.gotoScene("bite\_Screen\_1") end go\_to\_Level2\_Timer = timer.performWithDelay( 15000, go\_to\_Level2, 1 ) --End Game------------------------------------------------------------------------------------------ function KILL\_TIMERS () timer.cancel( game\_Time\_Timer ) timer.cancel( move\_Ketchup\_Timer ) timer.cancel( spawn\_Timer ) timer.cancel( go\_to\_Level2\_Timer ) enemy\_Drop:removeSelf( ) end end

Yea I used that, and it fixed the restarting, but the drop still spawns. Heres the bulk of the code. Also, thank you so much! 

Perhaps you should kill the timers before you try to go to the next scene?

Rob

I don’t see anywhere above where you are using the timer logic from the other thread. What I suggested there is a really good way to tag timers and have them all cancel when your scene changes. I’m not saying it’s the best way, but it’s one that has the least overhead.

Did you use a timer cancelling function like I suggested here? If so, can you provide that code so we can take a look at what the problem might be?

function scene:enterScene( event ) local group = self.view --Timers------------------------------------------------------------------------------ function game\_Time () time = time + 1 print("Time: "..time) end game\_Time\_Timer = timer.performWithDelay( 1000, game\_Time, 0 ) --Spawn Harder----------------------------------------------------------------------------- function drop\_Faster () gravity\_Speed = gravity\_Speed + 7 physics.setGravity( 0, gravity\_Speed ) print("Gravity Speed: "..gravity\_Speed) end drop\_Faster\_Timer = timer.performWithDelay( 5000, drop\_Faster, 2 ) --Characters------------------------------------------------------------------------------ function move\_Ketchup () ketchup\_X = math.random(20, 240 ) transition.to( enemy\_Ketchup, {x = ketchup\_X, time = 850} ) end move\_Ketchup\_Timer = timer.performWithDelay( 900, move\_Ketchup, 0 ) function move\_Fry (event) if ( event.phase == "moved" ) then fry\_Physical.x = player\_Fry.x - 60 hand\_Physical\_Diagonal.x = player\_Fry.x + 25 hand\_Physical\_Flat.x = player\_Fry.x + 70 player\_Fry.x = event.x end end Runtime:addEventListener( "touch", move\_Fry ) --Drops------------------------------------------------------------------------------ function spawn\_Drop (event) enemy\_Drop = display.newImage( "Assets/game\_Screen/FKdrop.png" ) enemy\_Drop.x = enemy\_Ketchup.x + 70 enemy\_Drop.y = 70 enemy\_Drop.Name = "drop" physics.addBody(enemy\_Drop, "dynamic") group:insert(enemy\_Drop) end spawn\_Timer = timer.performWithDelay( spawn\_Speed, spawn\_Drop, 0) local function removeOffscreenItems() for i = 1, #all\_Drops do local oneDrop = all\_Drops[i] if (oneDrop and oneDrop.y) then if oneDrop.y \> screenHeight then oneDrop:removeSelf() table.remove( all\_Drops, i ) print("delete") lives = lives - 1 print("Lives: "..lives) audio.play(missed\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end end end end end Runtime:addEventListener( "enterFrame", removeOffscreenItems ) --Collisions------------------------------------------------------------------------------ function onCollision( event) if ( event.phase == "began" ) then print( "began: " .. event.object1.Name .. " & " .. event.object2.Name ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.Name .. " & " .. event.object2.Name ) end if ( event.phase == "began" ) then if (event.object1.Name == "fry" and event.object2.Name == "drop") then system.vibrate() print("Lives: "..lives) CallKillPlayerEndGame(event.object2); audio.play(ding\_Sound, {loops = 0}) drops\_Caught = drops\_Caught + 1 drops\_Caught\_text.text = "Drops Caught: "..drops\_Caught elseif (event.object1.Name == "drop" and event.object2.Name == "fry") then system.vibrate() print("Lives: "..lives) CallKillPlayerEndGame(event.object1); audio.play(ding\_Sound, {loops = 0}) drops\_Caught = drops\_Caught + 1 drops\_Caught\_text.text = "Drops Caught: "..drops\_Caught --enemy\_Drop:removeSelf() elseif (event.object1.Name == "hand\_flat" and event.object2.Name == "drop") then lives = lives - 1 print( "Lives: "..lives ) CallKillPlayerEndGame(event.object2); REMOVE\_SPLAT() hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x hand\_Splat.y = event.object2.y + 40 group:insert(hand\_Splat) audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end elseif (event.object1.Name == "drop" and event.object2.Name == "hand\_flat") then lives = lives - 1 print( "Lives: "..lives ) CallKillPlayerEndGame(event.object1); REMOVE\_SPLAT() hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x hand\_Splat.y = event.object2.y + 40 group:insert(hand\_Splat) audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end elseif (event.object1.Name == "hand\_diagonal" and event.object2.Name == "drop") then hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x + 20 hand\_Splat.y = event.object2.y + 20 hand\_Splat.rotation = -45 group:insert(hand\_Splat) REMOVE\_SPLAT() lives = lives - 1 print( "Lives: "..lives) CallKillPlayerEndGame(event.object2); audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end elseif (event.object1.Name == "drop" and event.object2.Name == "hand\_diagonal") then hand\_Splat = display.newImage( "Assets/game\_Screen/FK\_handSplat.png" ) hand\_Splat.x = event.object2.x + 20 hand\_Splat.y = event.object2.y + 20 hand\_Splat.rotation = -45 group:insert(hand\_Splat) REMOVE\_SPLAT() lives = lives - 1 print( "Lives: "..lives ) CallKillPlayerEndGame(event.object1); audio.play(splat\_Sound, {loops = 0}) if ( lives == 0 ) then storyboard.gotoScene("lose\_Page", "fade") KILL\_TIMERS() end end end end Runtime:addEventListener("collision", onCollision) function CallKillPlayerEndGame(enemytoKill) --Used a timer to execute to kill the enemy - because you can not remove an object during the --Collision so we execute a small delay to allow the collision to complete. timer.performWithDelay( 1, function() enemytoKill:removeSelf() end ) end function REMOVE\_SPLAT () timer.performWithDelay( 800, function() hand\_Splat:removeSelf() end ) end --Level 2------------------------------------------------------------------------------------------ function go\_to\_Level2 () storyboard.gotoScene("bite\_Screen\_1") end go\_to\_Level2\_Timer = timer.performWithDelay( 15000, go\_to\_Level2, 1 ) --End Game------------------------------------------------------------------------------------------ function KILL\_TIMERS () timer.cancel( game\_Time\_Timer ) timer.cancel( move\_Ketchup\_Timer ) timer.cancel( spawn\_Timer ) timer.cancel( go\_to\_Level2\_Timer ) enemy\_Drop:removeSelf( ) end end

Yea I used that, and it fixed the restarting, but the drop still spawns. Heres the bulk of the code. Also, thank you so much! 

Perhaps you should kill the timers before you try to go to the next scene?

Rob

I don’t see anywhere above where you are using the timer logic from the other thread. What I suggested there is a really good way to tag timers and have them all cancel when your scene changes. I’m not saying it’s the best way, but it’s one that has the least overhead.