Physics Bodies Not Being Destroyed On Scene Destroy

I’ve been reading just about every single thread similar to my question and can’t find a solution to my problem.

 

I understand that when a SceneGroup is removed, all objects in it are removed as well.

This works!

However, when calling composer.gotoScene(“menu”), I am still seeing all of the physics bodies of the objects from “level1” while in “hybrid” mode.

 

Would you be able to help me figure out how to get rid of these?

The image is being removed, however the display body remains

 

Here is a snippet of my code.

[lua]

–this is in scene create

    sceneGroup:insert( background )

    sceneGroup:insert( grass)

    sceneGroup:insert( boat )

    sceneGroup:insert(scoreText)

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if event.phase == “will” then

  spawnController(“stop”, spawnParams)

physics.stop()

elseif phase == “did” then

end

end

function scene:destroy( event )

Runtime:removeEventListener(“touch”, moveBoat)

Runtime:removeEventListener(“collision”, onCollision)

for id, value in pairs(timer._runlist) do

timer.cancel(value)

end

display.remove(lifeRing)

display.remove(lifeRing2)

display.remove(lifeRing3)

display.remove(score)

scoreText = nil

package.loaded[physics] = nil

physics = nil

local sceneGroup = self.view

display.remove(sceneGroup)

–boat:removeSelf()

–grass:removeSelf()

end

[/lua]

Hi @m4rkmckinney,

I have two pieces of advice for you to try:

  1. Do not call “physics.stop()”. This API is rarely needed, unless you absolutely never intend to use physics in other parts of the app. If you intend to return (at some point) to the scene with physics, you should call “physics.pause()” instead.

  2. You should not try to remove the physics “package”. That is not necessary and it might be causing issues with your app.

Hope this helps,

Brent

Brent,

I appreciate the advice!

I went ahead and took out the two lines of questionable code.

However, when the scene is removed, I am still seeing the boat and grass physics bodies remaining on the screen.

Do you recall any other case where these bodies still exist even though their images have been removed from the display?

Thanks,

Mark

Here is a chunk of my code inside scene:create for reference.

[lua]

 function scene:create( event )

local sceneGroup = self.view

–Create Bounding Walls

local leftWall = display.newRect(0, 0, 1, display.contentHeight*2)

local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight*2)

–local ceiling = display.newRect(0, 0, display.contentWidth, 1)

leftWall.objectType = “leftWall”

rightWall.objectType = “rightWall”

–Convert Walls into physical bodies

physics.addBody(leftWall, “static”)

physics.addBody(rightWall, “static”)

table.insert(physicsDisplayObjects, leftWall)

table.insert(physicsDisplayObjects, rightWall)

lives = 3

local background = display.newImageRect( “sf_background.png”, screenW, screenH )

background.anchorX = 0

background.anchorY = 0

background:setFillColor( 1 )

    --BOAT–

boat = display.newImageRect(“boat.png”,100, 60)

boat.x, boat.y = screenW/2, screenH-113

local boatCollisionFilter = { categoryBits = 2, maskBits = 5 }

Runtime:addEventListener(“touch”, moveBoat)

boat.bodyActive = false

boat.objectType = “boat”

physics.addBody( boat, “dynamic”, {filter = boatCollisionFilter, bounce = 0} )

table.insert(physicsDisplayObjects, boat)

lifeRing = display.newImageRect(“life_ring.png”, 35, 35)

lifeRing.x = display.contentCenterX - 40

lifeRing.y = screenH - 50

lifeRing2 = display.newImageRect(“life_ring.png”, 35, 35)

lifeRing2.x = display.contentCenterX

lifeRing2.y = screenH - 50

lifeRing3 = display.newImageRect(“life_ring.png”, 35, 35)

lifeRing3.x = display.contentCenterX + 40

lifeRing3.y = screenH - 50

local grass = display.newImageRect( “waves0001.png”, screenW, 275 )

grass.anchorX = 0

grass.anchorY = 1

grass.x, grass.y = 0, display.contentHeight

grass.objectType = “grass”

– {botLeft, botRight, topRight, topLeft} Higher y = down

local grassShape = { -halfW, 80, halfW,80, halfW,55, -halfW,55 }

physics.addBody( grass, “static”, { friction=0.3, shape=grassShape} )

table.insert(physicsDisplayObjects, grass)

sceneGroup:insert( background )

sceneGroup:insert( grass)

sceneGroup:insert( boat )

sceneGroup:insert(scoreText)

spawnController(“start”, spawnParams)

end

[/lua]

Hi Mark,

Are you outright destroying the scene view, or only hiding it? Take a look at the description here:

https://docs.coronalabs.com/api/library/composer/recycleOnSceneChange.html

Brent

I don’t plan on reusing the scene again.

So ideally I would like to just destroy it entirely.

You actually answered my question earlier. Thank you!

Hi @m4rkmckinney,

I have two pieces of advice for you to try:

  1. Do not call “physics.stop()”. This API is rarely needed, unless you absolutely never intend to use physics in other parts of the app. If you intend to return (at some point) to the scene with physics, you should call “physics.pause()” instead.

  2. You should not try to remove the physics “package”. That is not necessary and it might be causing issues with your app.

Hope this helps,

Brent

Brent,

I appreciate the advice!

I went ahead and took out the two lines of questionable code.

However, when the scene is removed, I am still seeing the boat and grass physics bodies remaining on the screen.

Do you recall any other case where these bodies still exist even though their images have been removed from the display?

Thanks,

Mark

Here is a chunk of my code inside scene:create for reference.

[lua]

 function scene:create( event )

local sceneGroup = self.view

–Create Bounding Walls

local leftWall = display.newRect(0, 0, 1, display.contentHeight*2)

local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight*2)

–local ceiling = display.newRect(0, 0, display.contentWidth, 1)

leftWall.objectType = “leftWall”

rightWall.objectType = “rightWall”

–Convert Walls into physical bodies

physics.addBody(leftWall, “static”)

physics.addBody(rightWall, “static”)

table.insert(physicsDisplayObjects, leftWall)

table.insert(physicsDisplayObjects, rightWall)

lives = 3

local background = display.newImageRect( “sf_background.png”, screenW, screenH )

background.anchorX = 0

background.anchorY = 0

background:setFillColor( 1 )

    --BOAT–

boat = display.newImageRect(“boat.png”,100, 60)

boat.x, boat.y = screenW/2, screenH-113

local boatCollisionFilter = { categoryBits = 2, maskBits = 5 }

Runtime:addEventListener(“touch”, moveBoat)

boat.bodyActive = false

boat.objectType = “boat”

physics.addBody( boat, “dynamic”, {filter = boatCollisionFilter, bounce = 0} )

table.insert(physicsDisplayObjects, boat)

lifeRing = display.newImageRect(“life_ring.png”, 35, 35)

lifeRing.x = display.contentCenterX - 40

lifeRing.y = screenH - 50

lifeRing2 = display.newImageRect(“life_ring.png”, 35, 35)

lifeRing2.x = display.contentCenterX

lifeRing2.y = screenH - 50

lifeRing3 = display.newImageRect(“life_ring.png”, 35, 35)

lifeRing3.x = display.contentCenterX + 40

lifeRing3.y = screenH - 50

local grass = display.newImageRect( “waves0001.png”, screenW, 275 )

grass.anchorX = 0

grass.anchorY = 1

grass.x, grass.y = 0, display.contentHeight

grass.objectType = “grass”

– {botLeft, botRight, topRight, topLeft} Higher y = down

local grassShape = { -halfW, 80, halfW,80, halfW,55, -halfW,55 }

physics.addBody( grass, “static”, { friction=0.3, shape=grassShape} )

table.insert(physicsDisplayObjects, grass)

sceneGroup:insert( background )

sceneGroup:insert( grass)

sceneGroup:insert( boat )

sceneGroup:insert(scoreText)

spawnController(“start”, spawnParams)

end

[/lua]

Hi Mark,

Are you outright destroying the scene view, or only hiding it? Take a look at the description here:

https://docs.coronalabs.com/api/library/composer/recycleOnSceneChange.html

Brent

I don’t plan on reusing the scene again.

So ideally I would like to just destroy it entirely.

You actually answered my question earlier. Thank you!