Composer - remove scene question

Hi,

I just swiched from storyboard to composer and I have a question about removing scenes:

Why do I need to manually remove display objects which are inserted into the scene group? 

If I don’t remove them manually and just call composer.removeScene() they stay on screen.

[lua]

– Called when scene is destroyed

function scene:destroy( event )

   – Remove display objects and listeners

   borders:remove()

   ball:remove()

   swipe:removeListener()

   local sceneGroup = self.view

   log:log("Scene destroyed: " … composer.getSceneName( “current” ))

end

[/lua]

Daniel

Not sure exactly how you are building your objects but if they are local then they wouldn’t be removed.

I tend to add all my objects etc. to a table so when I go into destroy i can just loop through them and make sure everything is cleared regardless of what anything else is doing.

You can also add composer.recycleOnSceneChange = true which will remove the self.view from memory but find it is easier and cleaner to manually remove stuff like a so.

local objCache = {} local myObject = display.newRect() objCache[#objCache + 1] = myObject then in your destroy just say something like this. --Note I use pcall because I don't care if there is any kind of error just keep moving --the reason being is if you have removed the object elsewhere you could get error on removeSelf. for i = 1, #objCache do pcall(function() objCache[i]:removeSelf() end) pcall(function() objCache[i] = nil end) end

I normally wrap all this into some kind of cleanup function, but is useful for things like timers, particles etc. and so forth.

If they are on the screen after changing scenes, then you did not insert them in to the sceneGroup (The scene’s view)

Rob

I thought that should be the expected behavior.

I have passed sceneGroup as an argument to a function in a different module. Is that causing the problem?

How should sceneGroup be referenced from different modules?

Daniel

What are you doing with the sceneGroup in another module?

Creating display objects and inserting them into sceneGroup.

[lua]

– Called when scene is created

function scene:create( event )

   local sceneGroup = self.view

   log:log("Scene created: " … composer.getSceneName( “current” ))

   --physics.setDrawMode( “hybrid” )

   physics.setGravity( 0, 0 )

   physics:start()

   physics.pause()

   

   ball:create(colors, sceneGroup) – example, here I create a ball display object and add it to sceneGroup

   borders:setup(lineStrokeWidth, colors, sceneGroup)

   timer.performWithDelay(1000, function() 

         physics:start()

         swipe:addListener()

         ball:move()

      end)

end

[/lua]

Should I make a sample and report this as a bug?

Daniel

Hi Daniel,

I don’t understand how this would be considered a bug. What are you doing in the other module? If you’re correctly passing the view reference over, and then inserting objects into that view, it should be working fine. I think we need more specific details on the issue here.

Thanks,

Brent

I made a sample project (in attachment).

In the sample I add a display object in another module (passed reference to scene group). I insert the display object into the scene group. But when I destroy the scene, the object is still on screen. 

What am I doing wrong? I am using Corona 2014.2382.

I know I can delete the object myself, but I am confused by this behavior :slight_smile: Tnx 4 help.

Daniel

The object is being removed.  What’s not being removed is the physics body and I’m not sure why.  I’m building a physics based game now and I’m not doing anything to remove the bodies other than going to the new scene and when in hybrid display  mode, the objects physics bodies go away.

Also as a test,  I created the box directly in the create scene and it shows the same behavior.

Weird.  I’ll continue to look at it.  It’s got to be something simple. 

Not sure exactly how you are building your objects but if they are local then they wouldn’t be removed.

I tend to add all my objects etc. to a table so when I go into destroy i can just loop through them and make sure everything is cleared regardless of what anything else is doing.

You can also add composer.recycleOnSceneChange = true which will remove the self.view from memory but find it is easier and cleaner to manually remove stuff like a so.

local objCache = {} local myObject = display.newRect() objCache[#objCache + 1] = myObject then in your destroy just say something like this. --Note I use pcall because I don't care if there is any kind of error just keep moving --the reason being is if you have removed the object elsewhere you could get error on removeSelf. for i = 1, #objCache do pcall(function() objCache[i]:removeSelf() end) pcall(function() objCache[i] = nil end) end

I normally wrap all this into some kind of cleanup function, but is useful for things like timers, particles etc. and so forth.

If they are on the screen after changing scenes, then you did not insert them in to the sceneGroup (The scene’s view)

Rob

I thought that should be the expected behavior.

I have passed sceneGroup as an argument to a function in a different module. Is that causing the problem?

How should sceneGroup be referenced from different modules?

Daniel

What are you doing with the sceneGroup in another module?

Creating display objects and inserting them into sceneGroup.

[lua]

– Called when scene is created

function scene:create( event )

   local sceneGroup = self.view

   log:log("Scene created: " … composer.getSceneName( “current” ))

   --physics.setDrawMode( “hybrid” )

   physics.setGravity( 0, 0 )

   physics:start()

   physics.pause()

   

   ball:create(colors, sceneGroup) – example, here I create a ball display object and add it to sceneGroup

   borders:setup(lineStrokeWidth, colors, sceneGroup)

   timer.performWithDelay(1000, function() 

         physics:start()

         swipe:addListener()

         ball:move()

      end)

end

[/lua]

Should I make a sample and report this as a bug?

Daniel

Hi Daniel,

I don’t understand how this would be considered a bug. What are you doing in the other module? If you’re correctly passing the view reference over, and then inserting objects into that view, it should be working fine. I think we need more specific details on the issue here.

Thanks,

Brent

I made a sample project (in attachment).

In the sample I add a display object in another module (passed reference to scene group). I insert the display object into the scene group. But when I destroy the scene, the object is still on screen. 

What am I doing wrong? I am using Corona 2014.2382.

I know I can delete the object myself, but I am confused by this behavior :slight_smile: Tnx 4 help.

Daniel

The object is being removed.  What’s not being removed is the physics body and I’m not sure why.  I’m building a physics based game now and I’m not doing anything to remove the bodies other than going to the new scene and when in hybrid display  mode, the objects physics bodies go away.

Also as a test,  I created the box directly in the create scene and it shows the same behavior.

Weird.  I’ll continue to look at it.  It’s got to be something simple.