Please read the docs before you post here. It’s clearly stated that you need to supply BOTH x and y gravity values. I personally make sure that the Corona docs are as accurate as possible so that these type of questions don’t need to be asked, so please read them and learn from them.
It’s because there is nothing going on in your collision function. It just prints values, you have to put the code inside the onGlobalCollision function. Actually, a local collision function would probably be better to use on the player.
local function onPlayerCollision( event ) if event.phase =="began" then composer.gotoScene("restart", options) end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") player:addEventListener("onPlayerCollision") elseif ( phase == "did" ) then print("show showing objects") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) player:removeEventListener("onPlayerCollision") elseif ( phase == "did" ) then print("hide removing objects") Runtime:removeEventListener("touch", onTouch) end end
And I’m constantly getting this error :
ERROR: Runtime error 11:49:33.912 addEventListener: listener cannot be nil: nil 11:49:33.912 stack traceback: 11:49:33.912 [C]: in function 'error' 11:49:33.912 ?: in function 'getOrCreateTable' 11:49:33.912 ?: in function 'addEventListener' 11:49:33.912 ?: in function 'addEventListener' 11:49:33.912 ?: in function 'addEventListener' 11:49:33.912 C:\Users\user\Documents\Corona Projects\app\game.lua:96: in function '?' 11:49:33.912 ?: in function 'dispatchEvent' 11:49:33.912 ?: in function '\_nextTransition' 11:49:33.912 ?: in function \<?:1492\> 11:49:33.912 (tail call): ? 11:49:33.912 ?: in function '?' 11:49:33.912 ?: in function \<?:182\> 11:49:35.521
Nearly there. You’ve told it to look for a collision, but not where to go when it detects one. Look in the API refs for event listeners and read the bit about how to call them properly…
local function onPlayerCollision( event ) if event.phase =="began" then print("Touched") end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") elseif ( phase == "did" ) then print("show showing objects") player:addEventListener("collision", onPlayerCollision) Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) elseif ( phase == "did" ) then print("hide removing objects") player:addEventListener("collision", onPlayerCollision) Runtime:removeEventListener("touch", onTouch) end end
And now nothing is happening no errors or anything
local function onPlayerCollision( event ) if event.phase =="began" then print("Touched") end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") elseif ( phase == "did" ) then print("show showing objects") player.collision = onPlayerCollision player:addEventListener("collision") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) elseif ( phase == "did" ) then print("hide removing objects") player.collision = onPlayerCollision player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) end end
And still nothing happened . The console didn’t print anything .