Help with collision needed

I am creating a game with a ball and a player and when the ball touches the player , the restart.lua scene shows up . But when the ball touches the player nothing happens . Can someone help me with this ?

game.lua :

 ball = display.newImage("ball1.png") ball.x = 400 ball.y = 160 ball.myName = "ball" player = display.newImage("player.png") player.x = 100 player.y = 300 player.myName = "player" local function loopObject() local objectDown= function() transition.to(player, { time=600, y=20, onComplete=loopObject }) end transition.to(player, { time=600, y=300, onComplete=objectDown }) end loopObject() local options = { effect = "fade", time = 400 } local function onGlobalCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.myName .. " and " .. event.object2.myName ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName ) end end

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.

Like this ?

local function onPlayerCollision( event ) if event.phase =="began" then composer.gotoScene("restart", options) end end

Yes, and make sure to add the eventListener:

player:addEventListener("collision")

This is what I have :

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 

This is line 96 :

player:addEventListener("onPlayerCollision")

I also tried :

 player:addEventListener("onCollision")

Sorry my bad:

player.collision = onPlayerCollision player:addEventListener("collision")

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…

You can also do:

player:addEventListener("collision", onPlayerCollision)

I have this :

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

First of all on if phase == “did”, you have to remove the collision listener, not add it again.

Also, when you say nothing is happening, did the player ever collide with anything?

Try replacing this:

player:addEventListener("collision", onPLayerCollision)

with this:

player.collision = onPlayerCollision player:addEventListener("collision")

Yes the player collided with the ball . 

I have this now :

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 .

The line that says: 

local function onPLayerCollision(event)

Add self as a parameter too:

local function onPlayerCollision(self, event)

Still nothing in the console :

local function onPlayerCollision( self, event ) if event.phase =="began" then print("Touched") end end

Let me know if you need more code .

Really? Can you send me your project zipped up?

I don’t have zip . I hate it .

But I can give you the code in my game.lua

Oh. I have started doing this:

function anim:collision(event) if event.phase == "began" then print("Touched") end end anim:addEventListener("collision")

Ok.