Removing Items in Composer

 I’m using Composer.  Do I:

  1. need to remove timers in Scene Destroy?  If so how do I do it?

  2. need to remove functions and events?   I think they are handled by composer but wanted to confirm. 

     If so how do I do it?

  1. need to remove a local sensor radius?    example:  local sensorRadius = 30

    If so how do I do it?

  1. need to remove text objects?  If so how do I do it?

I tried totalrockText: removeSelf()   with totalrockText = nil and got an error message.

      example:   totalrockText = display.newText(totalrocks,0, 0, native.systemFontBold, 30 )

Thanks in advance and have a great day.

Lori            

  1. Timers should be removed once you’ve finished with them, but, yes, if they are long running you should call timer.cancel( timerId ) and set timerId = nil in sceneDestroy.
  2. No, they are not handled by composer. You need to remove listeners. The only listeners you don’t need to explicitly remove are touch and tap listeners - but only if they display objects they are listening to are removed when the scene is destroyed. Any display objects created in a scene which are not added to the scene’s view will not be automatically removed by composer.
  3. All local variables will be cleaned up when their scope is removed. If you create a variable in the sceneCreate event it will only exist within that function unless you pass to out somehow. You can remove references to object by optionally cancelling/removing the transition, timer, physics joint or listener they represent and setting their value to nil.
  4. Text objects are display objects like any other, so will be subject to the same rules. They will be removed if there are no non-touch/tap listeners listening to them and they or their parent group is removed.

I don’t understand what you’re trying to do in your code snippet. Please provide some extra context around it (and wrap it in a code snipper with the <> button.)

Read more on listeners here: http://docs.coronalabs.com/daily/guide/events/detectEvents/index.html#registering-events (Read the section titled “Important” for information about touch and tap listeners.)

Sorry if my terminology is off - I’m still in the learning mode.

I have  removed all my event listeners.  I’m talking about functions that for example calculate a score using variables I’ve created.  There is no event listener on the function.   Or, the function to which the event listener is tied to.  I’ve removed the listener I wasn’t sure about the function that goes with the listener.

The code snippet.  When I’m removing objects I use: 

rock: removeSelf()

rock = nil

But I haven’t done anything like that for text objects.  I didn’t know if I need to remove them in Scene Destroy as well.

Code in Scene Create:

totalRocksText = display.newText(totalRocks,0, 0, native.systemFontBold, 30 )
totalRocksText.isVisible = false
sceneGroup:insert(totalRocksText)
totalRocksText.text=totalRocks
totalRocksText:setFillColor( 255, 255, 255, 255 )
totalRocksText.x = 90; totalRocksText.y =280; totalRocksText.rotation = 270

Thank you for the fast response!

If your rock variable has a scope which ends when the scene ends, then it will get cleaned up. If you have the rock variable declared at the top of your scene module but outside of any functions, then you will need to explicitly clean it up. This goes for any variable.

The way I have my variables cleaned up is to have them as members of display groups and those display groups only ever added to other display groups. I try to avoid have direct variable references. If I do, it is really only as a named member of a table, not a free-standing variable (unless it is a local variable inside a small function performing a temporary job.)

I’ve put my variables in sceneGroup like below in Scene Create.  To make sure I’m grasping what you are saying, because I put the variable in the Forward Declarations area I will need to explicitly clean it up.   But, if I don’t have it in the Forward Declarations, because I input it into sceneGroup, Composer will automatically clean it up?      Would that make it a global variable?   Thank you for the help.

Forward Declarations:

local rock2

Scene Create:
rock2 =display.newImage(“Images/rockB.png”)
sceneGroup:insert(rock2)
 

Scene Destroy:

rock2: removeSelf()

rock2 = nil

It would not be a global variable if you only put it in the scene group without a variable in the forward declarations.

As it is above your variable will get cleaned up.

If you do not set it to nil in the destroy function it will not get cleaned up.

Awesome.   Looks like I’m doing double work right now.    So it would not be a global variable because of the “local” in front of the sceneGroup = self.view in Scene Create?  And it is destroyed because Composer has the sceneGroup in --Scene Destroy?

Thank you for your patience with my questions.


–SCENE CREATE

function scene:create( event )

local sceneGroup = self.view


–SCENE DESTROY

function scene:destroy( event )
local sceneGroup = self.view

I think you should take a look at this post: http://forums.coronalabs.com/topic/53698-transitions-and-timers/#entry279184

You’re right: Variables are only global if they do not have local in front.

  1. Timers should be removed once you’ve finished with them, but, yes, if they are long running you should call timer.cancel( timerId ) and set timerId = nil in sceneDestroy.
  2. No, they are not handled by composer. You need to remove listeners. The only listeners you don’t need to explicitly remove are touch and tap listeners - but only if they display objects they are listening to are removed when the scene is destroyed. Any display objects created in a scene which are not added to the scene’s view will not be automatically removed by composer.
  3. All local variables will be cleaned up when their scope is removed. If you create a variable in the sceneCreate event it will only exist within that function unless you pass to out somehow. You can remove references to object by optionally cancelling/removing the transition, timer, physics joint or listener they represent and setting their value to nil.
  4. Text objects are display objects like any other, so will be subject to the same rules. They will be removed if there are no non-touch/tap listeners listening to them and they or their parent group is removed.

I don’t understand what you’re trying to do in your code snippet. Please provide some extra context around it (and wrap it in a code snipper with the <> button.)

Read more on listeners here: http://docs.coronalabs.com/daily/guide/events/detectEvents/index.html#registering-events (Read the section titled “Important” for information about touch and tap listeners.)

Sorry if my terminology is off - I’m still in the learning mode.

I have  removed all my event listeners.  I’m talking about functions that for example calculate a score using variables I’ve created.  There is no event listener on the function.   Or, the function to which the event listener is tied to.  I’ve removed the listener I wasn’t sure about the function that goes with the listener.

The code snippet.  When I’m removing objects I use: 

rock: removeSelf()

rock = nil

But I haven’t done anything like that for text objects.  I didn’t know if I need to remove them in Scene Destroy as well.

Code in Scene Create:

totalRocksText = display.newText(totalRocks,0, 0, native.systemFontBold, 30 )
totalRocksText.isVisible = false
sceneGroup:insert(totalRocksText)
totalRocksText.text=totalRocks
totalRocksText:setFillColor( 255, 255, 255, 255 )
totalRocksText.x = 90; totalRocksText.y =280; totalRocksText.rotation = 270

Thank you for the fast response!

If your rock variable has a scope which ends when the scene ends, then it will get cleaned up. If you have the rock variable declared at the top of your scene module but outside of any functions, then you will need to explicitly clean it up. This goes for any variable.

The way I have my variables cleaned up is to have them as members of display groups and those display groups only ever added to other display groups. I try to avoid have direct variable references. If I do, it is really only as a named member of a table, not a free-standing variable (unless it is a local variable inside a small function performing a temporary job.)

I’ve put my variables in sceneGroup like below in Scene Create.  To make sure I’m grasping what you are saying, because I put the variable in the Forward Declarations area I will need to explicitly clean it up.   But, if I don’t have it in the Forward Declarations, because I input it into sceneGroup, Composer will automatically clean it up?      Would that make it a global variable?   Thank you for the help.

Forward Declarations:

local rock2

Scene Create:
rock2 =display.newImage(“Images/rockB.png”)
sceneGroup:insert(rock2)
 

Scene Destroy:

rock2: removeSelf()

rock2 = nil

It would not be a global variable if you only put it in the scene group without a variable in the forward declarations.

As it is above your variable will get cleaned up.

If you do not set it to nil in the destroy function it will not get cleaned up.

Awesome.   Looks like I’m doing double work right now.    So it would not be a global variable because of the “local” in front of the sceneGroup = self.view in Scene Create?  And it is destroyed because Composer has the sceneGroup in --Scene Destroy?

Thank you for your patience with my questions.


–SCENE CREATE

function scene:create( event )

local sceneGroup = self.view


–SCENE DESTROY

function scene:destroy( event )
local sceneGroup = self.view

I think you should take a look at this post: http://forums.coronalabs.com/topic/53698-transitions-and-timers/#entry279184

You’re right: Variables are only global if they do not have local in front.