[not solved] Nothing is disappearing when changing scene

I have my code set to change scene when reaching a certain number of points. For now, it’s set to change back to the main menu, just until I get the proper scene sorted. However, when the user clicks play (after  the scene change back to the main menu) All the objects are still there (stationary, so the physics stopped) and yet the code for spawning in the objects still runs. On top of this, the new objects that spawn in, don’t collide with the walls that are set there. Here’s my exitScene:

function scene:exitScene( event )     local group = self.view     local flygroup = self.view          physics.stop()       if (listenertimer) then         timer.cancel(listenertimer)     listenertimer = nil     end       if (spawntimer) then     timer.cancel(spawntimer)     spawntimer = nil     end          if (flygroup) then         for i=1,numFlies do          display.remove(flygroup)         flygroup = nil         print("group removed")         end     end       if (scorefield) then         scorefield:removeSelf()         scorefield = nil     end          if (spawnedflies) then         spawnedflies = 0     end              if (flying) then         for i=1,numFlies do          flying:removeSelf()         flying = nil         end     end          if (splat) then         splat:removeSelf()         splat = nil     end     end

I’m thinking I need to purge the data from the original scene completely, but I’m unsure how to do that.

When you create the display objects are you inserting them into the scene group? That needs to happen for them to go away when you exit the scene.

 Jay

I fixed it. I added some code it so that level1 is purged then removed once the scene changes. Unsure if it’s the correct method, but it appears to have worked.

    function flylistener()         if spawnedflies \> 15 then             numFlies = 0             i = 0             storyboard.gotoScene( "gameover", "fade", 500 )             storyboard.purgeScene("level1")             storyboard.removeScene("level1")         else     end     end

EDIT: Upon doing an actual device test, it would appear it’s not fixed. It doesn’t successfully transition back to the main menu.

  1. You only should run purge or remove, not both. purge will simply kill the scene object (leaving in place the rest of the code called in the scene file) whereas remove blows away all of it.

  2. When you’re removing groups of objects, I highly recommend you run it backwards.

[lua]for i = numFlies, 1, -1 do

  display.remove(flygroup)

  flygroup = nil

end[/lua]

That being said, there’s something really odd about your code. Assuming flygroup is a displayGroup containing all the flies, you should be doing this:

[lua]for i = 1, flygroup.numChildren, -1 do

  display.remove(flygroup[i])

end

display.remove(flygroup)[/lua]

Theoretically this is a much safer approach since it only relies on the size of the displayGroup.

(It’s probably also possible to just display.remove(flygroup) without an interator to kill all of the contents at once, though I don’t know the memory implications of that)

When you create the display objects are you inserting them into the scene group? That needs to happen for them to go away when you exit the scene.

 Jay

I fixed it. I added some code it so that level1 is purged then removed once the scene changes. Unsure if it’s the correct method, but it appears to have worked.

    function flylistener()         if spawnedflies \> 15 then             numFlies = 0             i = 0             storyboard.gotoScene( "gameover", "fade", 500 )             storyboard.purgeScene("level1")             storyboard.removeScene("level1")         else     end     end

EDIT: Upon doing an actual device test, it would appear it’s not fixed. It doesn’t successfully transition back to the main menu.

  1. You only should run purge or remove, not both. purge will simply kill the scene object (leaving in place the rest of the code called in the scene file) whereas remove blows away all of it.

  2. When you’re removing groups of objects, I highly recommend you run it backwards.

[lua]for i = numFlies, 1, -1 do

  display.remove(flygroup)

  flygroup = nil

end[/lua]

That being said, there’s something really odd about your code. Assuming flygroup is a displayGroup containing all the flies, you should be doing this:

[lua]for i = 1, flygroup.numChildren, -1 do

  display.remove(flygroup[i])

end

display.remove(flygroup)[/lua]

Theoretically this is a much safer approach since it only relies on the size of the displayGroup.

(It’s probably also possible to just display.remove(flygroup) without an interator to kill all of the contents at once, though I don’t know the memory implications of that)