The correct removal of unnecessary objects from the scene

Good day!

Since I’m new, please don’t be someone professional to answer my simple questions)))

1.  Whether this method:

        playBtn:removeSelf() – widgets must be manually removed

        playBtn = nil

   if Yes, then what is the point of the remote object to assign Nil?

   can first assign Nil and after a delete?

2.  In which part of the code, which function should be deleted?

     

      function scene:show( event )

or

       function scene:hide( event )

or

       function scene:destroy( event )

Thanks in advance for the detailed answer!

If you are leaving the scene, you don’t need to remove objects yourself. Add them to scene.view and composer will remove them automatically.

If you need to delete an object during a scene (i.e. a coin that was collected and is no longer needed), the safer option is:

display.remove(object) object = nil

This is safer because it will not cause a runtime error if the object no longer exists.

How about the fact that I downloaded the audio, used on stage, but when switching to another scene, the music continues sounds. The file is called as a function

local popugay\_zvuk = math.random(1,10) if ( popugay\_zvuk == 1) then audio.play( soundTable["sound\_hawk"] ) end

Good day!

Since I’m new, please don’t be someone professional to answer my simple questions)))

1.  Whether this method:

        playBtn:removeSelf() – widgets must be manually removed

Not all widgets are necessarily removed manually. As far as I know, only the native objects need to be removed manually. If you push your widgets created with Widget API(or any other display object) into the main group you created with scene.view, they will be removed.

For the other question, you need to remove all sound manually when you need to. Here is how you do it -> https://docs.coronalabs.com/api/library/audio/dispose.html

scene:show() and scene:hide() exist in pairs. What ever you start in scene:show() you should stop in scene:hide(). Corona’s scene management affects display objects inserted into the scene’s view group. Objects will show and hide automatically that you’ve added to the group. But they won’t be deleted until you call composer.removeScene(). You can set a flag that will cause Composer to automatically remove the scene on a scene change.

As for audio, if you load it in scene:show() then you should remove it in scene:hide(). If you load the audio in the scene’s main chunk or inside of scene:create(), then you would dispose of the audio in scene:destroy(). Note scene:destroy() only gets called as part of the composer.removeScene() call (or an auto-removal of the scene). If a sound is still playing, you should call audio.stop() inside of scene:hide().

Rob

And these four functions of “composer” must be used?

It turns out that the object declared in the function scene:create() was visible in the function scene:show(), it must be placed in a global variable.

Is it possible to use many global objects?

That’s a really broad question. To say must be used isn’t quite fair. Many scenes don’t have any work that needs done in :show() and :hide(). :destroy() is rarely used since many people have pre-loaded audio that they can play and stop in :show() and :hide() and there isn’t much left to do there.

As far as globals, you never ever need to use a global variable. You need to understand how local variables are scope. If you create something in :create() that you need to access in :show() you simply declare the variable “local” at the top of the scene and leave the “local” off when you create it:

local button function scene:create( event )       button = display.newImageRect("button.png", 32, 32) end function scene:show( event )      button.isVisible = false end

Rob

Thanks, ROB

If you are leaving the scene, you don’t need to remove objects yourself. Add them to scene.view and composer will remove them automatically.

If you need to delete an object during a scene (i.e. a coin that was collected and is no longer needed), the safer option is:

display.remove(object) object = nil

This is safer because it will not cause a runtime error if the object no longer exists.

How about the fact that I downloaded the audio, used on stage, but when switching to another scene, the music continues sounds. The file is called as a function

local popugay\_zvuk = math.random(1,10) if ( popugay\_zvuk == 1) then audio.play( soundTable["sound\_hawk"] ) end

Good day!

Since I’m new, please don’t be someone professional to answer my simple questions)))

1.  Whether this method:

        playBtn:removeSelf() – widgets must be manually removed

Not all widgets are necessarily removed manually. As far as I know, only the native objects need to be removed manually. If you push your widgets created with Widget API(or any other display object) into the main group you created with scene.view, they will be removed.

For the other question, you need to remove all sound manually when you need to. Here is how you do it -> https://docs.coronalabs.com/api/library/audio/dispose.html

scene:show() and scene:hide() exist in pairs. What ever you start in scene:show() you should stop in scene:hide(). Corona’s scene management affects display objects inserted into the scene’s view group. Objects will show and hide automatically that you’ve added to the group. But they won’t be deleted until you call composer.removeScene(). You can set a flag that will cause Composer to automatically remove the scene on a scene change.

As for audio, if you load it in scene:show() then you should remove it in scene:hide(). If you load the audio in the scene’s main chunk or inside of scene:create(), then you would dispose of the audio in scene:destroy(). Note scene:destroy() only gets called as part of the composer.removeScene() call (or an auto-removal of the scene). If a sound is still playing, you should call audio.stop() inside of scene:hide().

Rob

And these four functions of “composer” must be used?

It turns out that the object declared in the function scene:create() was visible in the function scene:show(), it must be placed in a global variable.

Is it possible to use many global objects?

That’s a really broad question. To say must be used isn’t quite fair. Many scenes don’t have any work that needs done in :show() and :hide(). :destroy() is rarely used since many people have pre-loaded audio that they can play and stop in :show() and :hide() and there isn’t much left to do there.

As far as globals, you never ever need to use a global variable. You need to understand how local variables are scope. If you create something in :create() that you need to access in :show() you simply declare the variable “local” at the top of the scene and leave the “local” off when you create it:

local button function scene:create( event )       button = display.newImageRect("button.png", 32, 32) end function scene:show( event )      button.isVisible = false end

Rob

Thanks, ROB