I have a main and two scenes. I can’t understand why composer doesn’t automatically remove the bodies behavior added with physics.addBody. According to http://forums.coronalabs.com/topic/37792-physics-bodies-still-remain-upon-switching-scenes/ physics body should be removed along with the display objects. In addition to the above link, I believe also the guy that posted http://forums.coronalabs.com/topic/21807-physics-body-goes-crazy-when-changing-scenes/ struggled with the same issue, but I’m guessing that he never turned on debug/hybrid draw mode and never realized that the bodies were going crazy because probably they were colliding with other invisibile objects from the previous scene. Here is my code.
main.lua
local composer = require("composer") composer.gotoScene("scene1")
scene1.lua
local composer = require("composer") local s = composer.newScene() function s:create(event) local group = self.view local physics = require("physics") physics.start() physics.setGravity(0, 1) physics.setDrawMode("hybrid") local t = display.newText(group, "Scene 1", display.contentWidth / 2, 20, native.systemFont, 16) local widget = require("widget") local button = widget.newButton { label = "Go to scene 2", onEvent = function (event) if event.phase == "ended" then composer.gotoScene("scene2") end end } button.x = display.contentWidth / 2 button.y = display.contentHeight / 2 - 100 group:insert(button) local body = display.newRect(group, 0, 0, 60, 30) body.x = display.contentWidth / 2 body.y = display.contentHeight / 2 physics.addBody(body, "dynamic") end s:addEventListener("create", s) return s
scene2.lua
local composer = require("composer") local s = composer.newScene() function s:create(event) local group = self.view local t = display.newText(group, "Scene 2", display.contentWidth / 2, 20, native.systemFont, 16) end s:addEventListener("create", s) return s
Among the things that fix the problem, are calling one of these functions right before gotoScene:
- group[3]:removeSelf()
- physics.removeBody(group[3])
- physics.stop()
Interestingly, the following call does’t fix the issue:
- group:removeSelf()
Is there an official guide/tutorial about using physics and composer together?