I have my main.lua pointing me to level one, i have an onCollision function which takes me to level two, but when i go to level two, everything loads fine except the physics engine. I get an error in the console saying ERROR: physics.start() has not been called. But at the top of level2.lua i have declared:
local physics = require( "physics" )
physics.start()
This is the end of my level1.lua file:
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
physics.stop()
elseif phase == "did" then
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
-- Called prior to the removal of scene's "view" (sceneGroup)
local sceneGroup = self.view
package.loaded[physics] = nil
physics = nil
end
The start of my level2.lua is this:
-----------------------------------------------------------------------------------
--
-- level2.lua
--
---------------------------------------------------------------------------------------
local composer = require( "composer" )
local scene = composer.newScene()
-- include Corona's "physics" library
local physics = require ("physics")
physics.start()
-- forward declarations and other locals
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth*0.5, display.contentHeight*0.5
local centerY, centerX = display.contentCenterY, display.contentCenterX
Why am i getting this error even though i have quite clearly started physics? I've tried copying the exact same code from level1.lua to level2.lua but im still getting the same results.
I had a collision listener in my scene:create function.
function onCollision( event )
if(event.object1.myName == "name" and event.object2.myName == "name" ) then
timer.performWithDelay( 3000, nextLevel, 1 )
return true
end
end
I think that this function kept on calling the physics package and returned the error when I progressed to the next level because I had called
physics.stop()
I managed to fix it by removing the event listener in line 4 of the function:
function onCollision( event )
if(event.object1.myName == "ball" and event.object2.myName == "finThree" ) then
timer.performWithDelay( 3000, nextLevel, 1 )
Runtime:removeEventListener( "collision", onCollision ) -- I remove the eventListener here.
return true
end
end
I still get the same error message as before, but the problem seems to be fixed. I know this won't be a fix for everyone, but it worked for me and hopefully it'll work for others suffering the same problem as me.
Thanks to Frozire for helping me.