Content isn't removed when changing scene

Corona isn’t removing my display object when I exitscene. Currently I have 2 images displayed in to variables and added to a group. In my exitscene I have the following:

        if (flygroup) then         flygroup:removeSelf()         flygroup = nil         print("group removed")     end        if (numFlies) then         numFlies = nil     end       if (topObject) then         topObject:removeSelf()         topObject = nil     end          if (flying) then         flying:removeSelf()         flying = nil     end    

Comodo is still acting up on the forums. Eurgh. Underneath the code block I did say that the same happens in destroyScene.

Are you inserting flyGroup and co into self.view?

The group is only set to self.view in my exitscene and destroyscene. If I set it to self.view when I create the group, the app doesn’t display properly.

What do you mean by "Doesn’t display properly’? When I use storyboard and have to use additional groups, I create the group and insert it into self.view straight after, with no consequences. Maybe it’s something to do with how you are positioning the group?

Displays correctly:

function spawn() &nbsp;&nbsp;&nbsp;&nbsp;numFlies = math.random(15,16) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;halfW = display.viewableContentWidth / 2 &nbsp;&nbsp;&nbsp;&nbsp;halfH = display.viewableContentHeight / 2 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for i=1,numFlies do&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;print("fly added") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flying = display.newImage("flyingfly.png", 12, 12) &nbsp;&nbsp;&nbsp;&nbsp;splat = display.newImage("flysplat.png",12,12) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup = display.newGroup() &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(flying, true) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(splat, true) &nbsp;&nbsp;&nbsp;&nbsp;splat.isVisible = false &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- Add to spawned flies count per fly added. &nbsp;&nbsp;&nbsp;&nbsp;spawnedflies = spawnedflies + 1 &nbsp;&nbsp;&nbsp;&nbsp;print(spawnedflies) &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- add physics to the fly, the filter is to assign them different bits so tehy don't collide. &nbsp;&nbsp;&nbsp;&nbsp;physics.addBody(flygroup, {bounce = 1, density = 1, filter = {maskBits = 2, categoryBits = 4}}) &nbsp;&nbsp;&nbsp;&nbsp;flygroup.gravityScale = 0 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) ) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:addEventListener( "touch", buttonListener ) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- set velocity in pixels per second. &nbsp;&nbsp;&nbsp;&nbsp;velocityx = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;velocityy = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:setLinearVelocity(velocityx,velocityy) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- If statement to detect which way the flies are moving and display the image accordingly. &nbsp;&nbsp;&nbsp;&nbsp;if velocityx \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(-1,1) &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(1,1) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;t = 1 &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;spawner() &nbsp;&nbsp;&nbsp;&nbsp;flylistener() end

cx7z.jpg

Broken:

function spawn() &nbsp;&nbsp;&nbsp;&nbsp;numFlies = math.random(15,16) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;halfW = display.viewableContentWidth / 2 &nbsp;&nbsp;&nbsp;&nbsp;halfH = display.viewableContentHeight / 2 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for i=1,numFlies do&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;print("fly added") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flying = display.newImage("flyingfly.png", 12, 12) &nbsp;&nbsp;&nbsp;&nbsp;splat = display.newImage("flysplat.png",12,12) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup = display.newGroup() &nbsp; &nbsp; flygroup = self.view &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(flying, true) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(splat, true) &nbsp;&nbsp;&nbsp;&nbsp;splat.isVisible = false &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- Add to spawned flies count per fly added. &nbsp;&nbsp;&nbsp;&nbsp;spawnedflies = spawnedflies + 1 &nbsp;&nbsp;&nbsp;&nbsp;print(spawnedflies) &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- add physics to the fly, the filter is to assign them different bits so tehy don't collide. &nbsp;&nbsp;&nbsp;&nbsp;physics.addBody(flygroup, {bounce = 1, density = 1, filter = {maskBits = 2, categoryBits = 4}}) &nbsp;&nbsp;&nbsp;&nbsp;flygroup.gravityScale = 0 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) ) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:addEventListener( "touch", buttonListener ) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- set velocity in pixels per second. &nbsp;&nbsp;&nbsp;&nbsp;velocityx = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;velocityy = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:setLinearVelocity(velocityx,velocityy) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- If statement to detect which way the flies are moving and display the image accordingly. &nbsp;&nbsp;&nbsp;&nbsp;if velocityx \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(-1,1) &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(1,1) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;t = 1 &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;spawner() &nbsp;&nbsp;&nbsp;&nbsp;flylistener() end

m8hz.jpg

This line is wrong:

flygroup = self.view

It should be:

self.view:insert(&nbsp;flygroup )

Also you should look into using local variables: http://www.lua.org/pil/4.2.html

Hope this helps :slight_smile:

And guess what, it works :).

Thanks, yeah I switched to global variables temporarily. the app isn’t ready for release yet. Just so I know for future reference, what do I need to manually assign nil to when I change a scene? I know widgets and display objects I do. Do I do the same for variables?

You are welcome.

It is recommended to set variables to nil also when you have finished using them (table’s especially). But you have to be aware of Lua scoping.

For instance, if you create a local variable inside your create scene function, you won’t be able to reference it inside your enter scene function and so on.

In this case you should probably have a local table above your create scene function that contains references to your objects, so you can remove them in your exitScene function.

Ex:

local myVars = { myTable = nil, flyGroup = nil, } function scene:createScene( event ) local flyGroup = display.newGroup() -- Set a reference myVars.flyGroup = flyGroup -- Create a table local myTable = {} -- Set a reference myVars.myTale = myTable end function scene:exitScene( event ) -- Nil references myVars.myTable = nil myVars.flyGroup = nil end

There are several ways of achieving this, and that is only a very basic example.

Hope this helps.

Got it, thanks. Just needed to know what items needed nullifying :).

Comodo is still acting up on the forums. Eurgh. Underneath the code block I did say that the same happens in destroyScene.

Are you inserting flyGroup and co into self.view?

The group is only set to self.view in my exitscene and destroyscene. If I set it to self.view when I create the group, the app doesn’t display properly.

What do you mean by "Doesn’t display properly’? When I use storyboard and have to use additional groups, I create the group and insert it into self.view straight after, with no consequences. Maybe it’s something to do with how you are positioning the group?

Displays correctly:

function spawn() &nbsp;&nbsp;&nbsp;&nbsp;numFlies = math.random(15,16) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;halfW = display.viewableContentWidth / 2 &nbsp;&nbsp;&nbsp;&nbsp;halfH = display.viewableContentHeight / 2 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for i=1,numFlies do&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;print("fly added") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flying = display.newImage("flyingfly.png", 12, 12) &nbsp;&nbsp;&nbsp;&nbsp;splat = display.newImage("flysplat.png",12,12) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup = display.newGroup() &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(flying, true) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(splat, true) &nbsp;&nbsp;&nbsp;&nbsp;splat.isVisible = false &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- Add to spawned flies count per fly added. &nbsp;&nbsp;&nbsp;&nbsp;spawnedflies = spawnedflies + 1 &nbsp;&nbsp;&nbsp;&nbsp;print(spawnedflies) &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- add physics to the fly, the filter is to assign them different bits so tehy don't collide. &nbsp;&nbsp;&nbsp;&nbsp;physics.addBody(flygroup, {bounce = 1, density = 1, filter = {maskBits = 2, categoryBits = 4}}) &nbsp;&nbsp;&nbsp;&nbsp;flygroup.gravityScale = 0 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) ) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:addEventListener( "touch", buttonListener ) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- set velocity in pixels per second. &nbsp;&nbsp;&nbsp;&nbsp;velocityx = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;velocityy = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:setLinearVelocity(velocityx,velocityy) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- If statement to detect which way the flies are moving and display the image accordingly. &nbsp;&nbsp;&nbsp;&nbsp;if velocityx \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(-1,1) &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(1,1) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;t = 1 &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;spawner() &nbsp;&nbsp;&nbsp;&nbsp;flylistener() end

cx7z.jpg

Broken:

function spawn() &nbsp;&nbsp;&nbsp;&nbsp;numFlies = math.random(15,16) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;halfW = display.viewableContentWidth / 2 &nbsp;&nbsp;&nbsp;&nbsp;halfH = display.viewableContentHeight / 2 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for i=1,numFlies do&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;print("fly added") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flying = display.newImage("flyingfly.png", 12, 12) &nbsp;&nbsp;&nbsp;&nbsp;splat = display.newImage("flysplat.png",12,12) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup = display.newGroup() &nbsp; &nbsp; flygroup = self.view &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(flying, true) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:insert(splat, true) &nbsp;&nbsp;&nbsp;&nbsp;splat.isVisible = false &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- Add to spawned flies count per fly added. &nbsp;&nbsp;&nbsp;&nbsp;spawnedflies = spawnedflies + 1 &nbsp;&nbsp;&nbsp;&nbsp;print(spawnedflies) &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- add physics to the fly, the filter is to assign them different bits so tehy don't collide. &nbsp;&nbsp;&nbsp;&nbsp;physics.addBody(flygroup, {bounce = 1, density = 1, filter = {maskBits = 2, categoryBits = 4}}) &nbsp;&nbsp;&nbsp;&nbsp;flygroup.gravityScale = 0 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;flygroup:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) ) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:addEventListener( "touch", buttonListener ) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- set velocity in pixels per second. &nbsp;&nbsp;&nbsp;&nbsp;velocityx = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;velocityy = math.random(-100,200) &nbsp;&nbsp;&nbsp;&nbsp;flygroup:setLinearVelocity(velocityx,velocityy) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- If statement to detect which way the flies are moving and display the image accordingly. &nbsp;&nbsp;&nbsp;&nbsp;if velocityx \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(-1,1) &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flygroup:scale(1,1) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;t = 1 &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;spawner() &nbsp;&nbsp;&nbsp;&nbsp;flylistener() end

m8hz.jpg

This line is wrong:

flygroup = self.view

It should be:

self.view:insert(&nbsp;flygroup )

Also you should look into using local variables: http://www.lua.org/pil/4.2.html

Hope this helps :slight_smile:

And guess what, it works :).

Thanks, yeah I switched to global variables temporarily. the app isn’t ready for release yet. Just so I know for future reference, what do I need to manually assign nil to when I change a scene? I know widgets and display objects I do. Do I do the same for variables?

You are welcome.

It is recommended to set variables to nil also when you have finished using them (table’s especially). But you have to be aware of Lua scoping.

For instance, if you create a local variable inside your create scene function, you won’t be able to reference it inside your enter scene function and so on.

In this case you should probably have a local table above your create scene function that contains references to your objects, so you can remove them in your exitScene function.

Ex:

local myVars = { myTable = nil, flyGroup = nil, } function scene:createScene( event ) local flyGroup = display.newGroup() -- Set a reference myVars.flyGroup = flyGroup -- Create a table local myTable = {} -- Set a reference myVars.myTale = myTable end function scene:exitScene( event ) -- Nil references myVars.myTable = nil myVars.flyGroup = nil end

There are several ways of achieving this, and that is only a very basic example.

Hope this helps.