How to properly remove a scene when exiting

Hi

I have scene which checks if images exist and displays them if they exists based on parameters when entering the scene. This works ok but when I try to re enter the same scene using different parameters the images are still there. How do I remove them when leaving the scene?

I am displaying the images in the scene:create section. I tried to remove the objects in the scene in the scene:destroy but it didn’t work.

Not sure how this would work

Thanks

Hi @marshallp24,

The first thing to always check in cases like this is that you’re properly adding/inserting the images into the scene’s “view” layer.

Best regards,

Brent

I’ve got all the objects in a scroll view and I’ve inserted the scrollview into a sceneGroup (self.view)

Is there something I should run as I leave the scene?

There are two parts to this:

First, when you put your objects in the scene’s view group, which it sounds as if you have done, then Composer **can** manage them for you. When you leave and enter scenes, it will show and hide the objects, transition them if you have transitions turned on etc. and if you trigger the scene to be removed, it will then remove the scene for you.

scene:create() builds the scene and keeps it in memory. If you leave the scene and come back, all of those objects are still cached and the scene is not re-created, just shown a second time. If you want to have a different scene when you come back you have to remove the scene, which will cause scene:create() to run again and build the new scene.

The simplest way to do this is to call:

composer.removeScene(“sceneName”) just before you call composer.gotoScene(“sceneName”). This will remove the display objects and when the scene is re-entered, it will be created using the new parameters you passed to it. scene:destroy() is called as part of composer.removeScene() to give you a chance to remove objects that are **not** display objects, like native things, audio, etc. You generally don’t do much in scene:destroy().

Rob

Hi @marshallp24,

The first thing to always check in cases like this is that you’re properly adding/inserting the images into the scene’s “view” layer.

Best regards,

Brent

I’ve got all the objects in a scroll view and I’ve inserted the scrollview into a sceneGroup (self.view)

Is there something I should run as I leave the scene?

There are two parts to this:

First, when you put your objects in the scene’s view group, which it sounds as if you have done, then Composer **can** manage them for you. When you leave and enter scenes, it will show and hide the objects, transition them if you have transitions turned on etc. and if you trigger the scene to be removed, it will then remove the scene for you.

scene:create() builds the scene and keeps it in memory. If you leave the scene and come back, all of those objects are still cached and the scene is not re-created, just shown a second time. If you want to have a different scene when you come back you have to remove the scene, which will cause scene:create() to run again and build the new scene.

The simplest way to do this is to call:

composer.removeScene(“sceneName”) just before you call composer.gotoScene(“sceneName”). This will remove the display objects and when the scene is re-entered, it will be created using the new parameters you passed to it. scene:destroy() is called as part of composer.removeScene() to give you a chance to remove objects that are **not** display objects, like native things, audio, etc. You generally don’t do much in scene:destroy().

Rob