I tried this :
function scene:create(event) local screenGroup = self.view local background = display.newImageRect("terrain.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) ball = display.newImage("ball1.png") ball.x = 400 ball.y = 160 physics.addBody(ball, "dynamic") screenGroup:insert(ball) line = display.newImage("line.png") line.x = 80 line.y = 200 line:scale( 0.1, 0.2 ) physics.addBody(line, "static", {density=.1, friction=.2, radius=12}) screenGroup:insert(line) player = display.newImage("player.png") player.x = 100 player.y = 300 physics.addBody(player, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(player) function player:collision(event) if event.phase == "began" then transition.cancel() composer.gotoScene("restart", options) end end local function loopObject() local objectDown= function() transition.to(player, { time=1600, y=20, onComplete=loopObject }) end transition.to(player, { time=1600, y=300, onComplete=objectDown }) end loopObject() end
local function onCollision() if event.phase == "began" then if event.object1 == "ball" and event.object2 == "line" then print("Touched") end end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then ball:addEventListener( "onCollision", onCollision) 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 elseif ( phase == "did" ) then print("hide removing objects") composer.removeScene( "start" ) ball:removeEventListener( "onCollision", onCollision ) player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) display.remove( ball ) display.remove( player ) end end
When I tried the above code , nothing happened I didn’t get any errors . And then I did this code below :
ball:addEventListener( "onCollision") ball:removeEventListener( "onCollision")
And I got this error :
ERROR: Runtime error addEventListener: listener cannot be nil: nil stack traceback: [C]: in function 'error' 10:58:37.860 ?: in function 'getOrCreateTable' 10:58:37.860 ?: in function 'addEventListener' 10:58:37.860 ?: in function 'addEventListener' 10:58:37.860 ?: in function 'addEventListener' 10:58:37.860 C:\Users\user\Documents\Corona Projects\app\game.lua:100: in function '?'
line 100 :
ball:addEventListener( "onCollision")